Thursday, May 19, 2011

Serving Django with CherryPy wsgiserver

For my current project, I do not need heavy duty webserver. What I needed was a simple (python) webserver and cherrypy wsgiserver does the job.

To make things simple:
copy cherrypy/wsgiserver/__init__.py into the project folder and rename it as wsgiserver.py.

Next step is to create a wsgiserver startup script. It would be something like the following:

1:  import wsgiserver  
2:  import sys  
3:  import os  
4:  import django.core.handlers.wsgi  
5:  if __name__ == "__main__":  
6:    sys.path.append(os.path.realpath(os.path.dirname(__file__)))  # add django project absolute path  
7:    # Startup Django  
8:    os.environ['DJANGO_SETTINGS_MODULE'] = 'django_project_dir_name.settings'  
9:    server = wsgiserver.CherryPyWSGIServer(  
10:      ('127.0.0.1', 8080),  
11:      django.core.handlers.wsgi.WSGIHandler()  
12:    )  
13:    try:  
14:      server.start()  
15:    except KeyboardInterrupt:  
16:      print 'Stopping'  
17:      server.stop()  


Of course the performance is below apache, lighttpd, nginx, but when one does not need high performance and would like a simple (wsgi) webserver, this is one of the answers.

2 comments: