How to store uScopy data in MondoDB: Difference between revisions

From KitwarePublic
Jump to navigationJump to search
No edit summary
No edit summary
 
Line 10: Line 10:
import sys
import sys


# add module path
sys.path.append('/var/www-dj/')
sys.path.append('/var/www-dj/')


# import module with singleton instance
from mongo import m
from mongo import m


def application(environ, start_response):
def application(environ, start_response):
         """ The WSGI test application """
         """ The WSGI application to retrieve images from database """
         def error(error_msg):
         def error(error_msg):
                 status = "200 OK"
                 status = "200 OK"
Line 26: Line 28:
         db = m.conn['clay1']
         db = m.conn['clay1']


        #print 'starting request ..'
         try:
         try:
                 str2 = environ['PATH_INFO']
                 str2 = environ['PATH_INFO']
         except:
         except:
                # Give some default request if
                # no parameters passed in URL
                 str2 = '/tiles3.py/n1/t.jpg'
                 str2 = '/tiles3.py/n1/t.jpg'


Line 48: Line 51:
                             ('Content-Length', str(len(res['file'])))]
                             ('Content-Length', str(len(res['file'])))]
         start_response(status, response_headers)
         start_response(status, response_headers)
        #print 'Started response'
         return [str(res['file'])]
         return [str(res['file'])]
        #except:
if __name__ == '__main__':
    # this runs when script is started directly from commandline
    try:
        # create a simple WSGI server and run the application
        from wsgiref import simple_server
        print "Running test application - point your browser at http://localhost:8000/ ..."
        httpd = simple_server.WSGIServer(('', 8000), simple_server.WSGIRequestHandler)
        httpd.set_app(application)
        httpd.serve_forever()
    except ImportError:
        # wsgiref not installed, just output html to stdout
                                                                                                                                                                                            1,1          Top
</source>
</source>



Latest revision as of 20:13, 18 January 2011

Server side code

Python code <source lang="python"> """

Decodes the path info and fetches the corresponding tile from pymongo

""" import pymongo import sys

  1. add module path

sys.path.append('/var/www-dj/')

  1. import module with singleton instance

from mongo import m

def application(environ, start_response):

       """ The WSGI application to retrieve images from database """
       def error(error_msg):
               status = "200 OK"
               response_headers = [('Content-type', 'text/plain'),
                           ('Content-Length', str(len(error_msg)))]
               start_response(status, response_headers)
               return [error_msg]
       # emit status / headers
       db = m.conn['clay1']
       try:
               str2 = environ['PATH_INFO']
       except:
               # Give some default request if 
               # no parameters passed in URL 
               str2 = '/tiles3.py/n1/t.jpg'
       path = str2.split('/')
       if len(path) != 3:
               #incorrect syntax
               return error(str(path))
       col = db[path[1]]
       res = col.find_one({"name" : path[2]})
       m.conn.end_request()
       if res == None:
               return error('Item not found in database ..')
       # Create the response headers
       status = "200 OK"
       response_headers = [('Content-type', 'image/jpeg'),
                           ('Content-Length', str(len(res['file'])))]
       start_response(status, response_headers)
       return [str(res['file'])]

</source>

Mongo module manages connection pool internally. Another simple script is used to recreate indexes after data has been added.



Demo