Hosting a Django Site With the CherryPy WSGI Server
So after hearing about CherryPy’s WSGI server while at PyCon (I went to the Pylons/TG2 classes), I decided, like others have, to see if I could host a Django site with it here where I work. There are several references out there about using that server code with Django, but I had a tough time getting to the point where I could actually use it to handle the Admin media. Some of the articles were slightly out of date with changes made to the most recent version of the CherryPy WSGI server, so while this is drawing off of several other folk’s work (like Eric Florenzano and DjangoCerise ), I present what I did to make it work while also realizing that this is probably still very incomplete. I am just hoping this will help someone get started.
Prior to the code, I downloaded just the wsgiserver.py from CherryPy. You can get the latest from trunk if you’re running Linux, with just a command like this:
wget http://svn.cherrypy.org/trunk/cherrypy/wsgiserver/__init__.py -O wsgiserver.py
Then I also wanted to have some logging capabilities, so I discovered Paste’s TransLogger middleware. All you have to do is download the translogger.py file from the paste project (sorry no quicky command for that, I grabbed it via the browser and I am too lazy to get you the command for that).
So with those 2 files (wsgiserver.py and translogger.py) now in the main directory of my particular django project (dash2 is the name in my example), I created a new cherryserve.py in that same directory as well.
import wsgiserver #This can be from cherrypy import wsgiserver if you're not running it standalone. import sys import os import django.core.handlers.wsgi from django.core.servers.basehttp import AdminMediaHandler from translogger import TransLogger if __name__ == "__main__": sys.path.append('/home/swilcox/projects') os.environ['DJANGO_SETTINGS_MODULE'] = 'dash2.settings' app = AdminMediaHandler(django.core.handlers.wsgi.WSGIHandler()) logged_app = TransLogger(app) server = wsgiserver.CherryPyWSGIServer( ('127.0.0.1', 8080), logged_app, server_name='luz.lifeway.org', numthreads = 20, ) try: server.start() except KeyboardInterrupt: server.stop()
So why did I do the path trickery up there? Well, because in my development environment, I don’t want my entire projects directory normally on my python path. Feel free to call me paranoid. But to get the server to work correctly with the django code, I needed it there. For a production server, the sys.path.append business wouldn’t normally need to be there, because I would have my apps or projects directory as part of the Python path.
Also note that if you needed to serve multiple apps, with this newer version of the wsgiserver.py, you would want to set them all up in a WSGIPathInfoDispatcher object first and then pass that in to the CherryPyWSGIServer instead of logged_app.
I think the other fields are pretty self-explanatory… ports, IP address, threads, etc…
The Admin Media trick is using app = AdminMediaHandler(django.core.handlers.wsgi.WSGIHandler()) instead of just the WSGIHandler by itself.
If you don’t want the logger, you just pass in app instead of logged_app.
2 Comments so far
Leave a reply
Inspired by your code snippet, I went digging further into AdminMediaHandler, turning it into a generic media handler that can be used to serve static files from Django/CherryPy: http://www.arteme.fi/blog/2009/02/26/django-cherrypy-dev-server-and-static-files
command for getting a copy of translogger.py from Paste without installing…:
wget http://svn.pythonpaste.org/Paste/trunk/paste/translogger.py -O translogger.py