Most people in the Django community are deploying their apps these days with mod_wsgi. If not, then you're at least using WSGI as a communication layer with your application server, in one way or another. The great thing about WSGI is that it gives everyone a common interface through which to talk. It also has the added benefit of being a common abstraction that many people have built these great, really useful tools on top of.
Consider Repoze. If you navigate to the middleware section of their website, they have some really cool stuff available! There are utilities for logging, authentication, security, profiling, templating, etc. All of these pieces of middleware are designed to be totally pluggable, because they are designed to work solely based on what's available through WSGI.
My personal favorite of that lot is repoze.profile. It accumulates Python profiling information about whatever app is being run, and allows you to view that profile information via a web interface by visiting a special URL. There is absolutely no reason that the Pylons, TurboGears, or CherryPy guys should be able to get away with keeping this stuff for themselves, so I want to show just how easy it is to integrate this profiling module with Django.
First, though, here's a typical .wsgi file that might be used in conjunction with mod_wsgi:
import os, sys
sys.stdout = sys.stderr
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
There's really nothing special going on here, and if you would like to learn more about how to set up this WSGI file, visit mod_wsgi's documentation on the subject. Now if you'll notice, application is simply an instance of WSGIHandler, which is simply a callable. A WSGI middleware is just a wrapper around that callable. Here's how easy it is to add the profiling middleware:
from repoze.profile.profiler import AccumulatingProfileMiddleware
application = AccumulatingProfileMiddleware(
application,
log_filename='/tmp/djangoprofile.log',
discard_first_request=True,
flush_at_shutdown=True,
path='/__profile__')
There we go! We have imported the profiling middleware, and passed the Django WSGI application as the first argument. The rest is just setting options for the middleware. You can restart apache and the WSGI profiling middleware is already working.
Sometimes, though, you don't want all of Apache just to run some middleware. You want to be able to do the same thing, but locally. Believe it or not, Django's local development server is just a WSGI server itself, so one option would be to do the wrapping directly in django, right here. But you really don't want to be hacking inside of Django internals if you don't have to. Fortunately there are many alternative WSGI servers out there. Brian Rosner has created a custom management command to use the excellent CherryPy WSGI server with Django, on his blog.
Let's say you just want to try this out quickly after reading this blog post, though. If you're running Python 2.5 or greater, you're in luck, because a script less than 10 lines long can get you up and running:
#!/usr/bin/env python
import sys
from wsgiref.simple_server import make_server
if __name__ == "__main__":
execfile(sys.argv[1])
httpd = make_server('', 8000, application)
httpd.serve_forever()
Now, to run it, simply invoke it like this:
python runserver.py my_wsgi_file.wsgi
Now, navigate around your app for a little bit and then point your browser to the profile url and see how freaking awesome middleware can be.
I'm not trying to stir up any controversy, I'm not saying we should stop making Django middleware or anything like that. But I seriously, seriously hope that someone tries this out and realizes the multitudes of great WSGI apps out there that can be taken advantage of. Mark Ramm wasn't full of hot air when he talked about this at DjangoCon or blogged about it later. He was right, and I for one wish I had listened sooner.
All Content


By V at 3:46 a.m. on Nov. 18, 2008
"You are right, and I for one wish I am listening now." :)
By Justin Lilly at 9:47 a.m. on Nov. 18, 2008
For those curious about what it looks like, check it out at http://bit.ly/C5qX
By Zeke Harris at 10:39 a.m. on Nov. 18, 2008
K, now I'm curious how I get a .wsgi to work with Lighttpd! Any thoughts? I haven't been able to find anything except running it through fastcgi (which requires you to run ./runserver startfcgi blah blah blah every time your server restarts and that sucks). I know lighttpd is wsgi compatible...but I just want to be able to point at a simple .wsgi in my config and have it start & restart when the server does.
By Erik Karulf at 2:52 p.m. on Nov. 18, 2008
I switched from lighttpd to nginx as a drop-in replacement while ago and I have been pretty pleased with the decision.
I would seriously recommend taking a look at Spawning. As far as restarting goes, I would just add a dependency on the Spawning application in the shutdown / startup hooks for the lighttpd/nginx rc script.
By Noah Gift at 9:42 p.m. on Nov. 18, 2008
This is a great point. I deployed a pretty big Django app this year using mod_wsgi/ with Apache forking only, not threading.
It seems like it is just a total shame if the larger web community can't reuse middleware written via the same spec. At PyWorks Jonathan LaCour mentioned something really sweet, that he has a WSGI app that commits all POST requests to the MySQL master, and the rest go to slaves.
By Arne Brodowski at 5:18 a.m. on Nov. 19, 2008
I haven't noticed it earlier, but in fact Django already uses a WSGI Middleware: If you use the runserver command to start the development server, a WSGI middleware is used to serve the admin-media.
For details see: django.core.servers.basehttp.AdminMediaHandler
So if you use the code above and wonder why your admin media isn't served just wrap the WSGIHandler in an AdminMediaHandler and it works like in the dev-server:
application = AdminMediaHandler(WSGIHandler())
By wholesale lingerie at 9:29 p.m. on May 15, 2009
it is just a total shame if the larger web community can't reuse middleware written via the same spec.
By ben 10 oyunları at 3:10 a.m. on May 25, 2009
I would seriously recommend taking a look at Spawning. As far as restarting goes, I would just add a dependency on the Spawning application in the shutdown / startup hooks for the lighttpd/nginx rc script.
By SGK at 2:52 p.m. on May 25, 2009
I switched from lighttpd to nginx as a drop-in replacement while ago and I have been pretty pleased with the decision.
Yeah
By Click to play at 10:37 p.m. on June 12, 2009
Interesting insight
By buy wow gold at 4:33 a.m. on June 13, 2009
Thank you for the specific info you providing in the post.
By University credit transfer at 4:05 a.m. on June 17, 2009
I would recommend take a look at Spawning.
By MUST financial Aid at 4:06 a.m. on June 17, 2009
very nice and interesting post thank you !
By Earn a High School Diploma at 4:07 a.m. on June 17, 2009
I will try this one tonight
By must university scam at 4:09 a.m. on June 17, 2009
doesnt work at my end :(
By hitloop at 1:37 p.m. on June 17, 2009
You are right, and a wish I am listening now. ))))
By Ambiccomses at 4:05 p.m. on June 20, 2009
<a href="http://profiles.friendster.com/108997025">NEXT DOOR NIKKI MOVIES</a>
http://profiles.friendster.com/108997025 - NEXT DOOR NIKKI MOVIES
<a href=http://profiles.friendster.com/108997025>NEXT DOOR NIKKI MOVIES</a>
http://profiles.friendster.com/108997025 - NEXT DOOR NIKKI MOVIES
<a href="http://profiles.friendster.com/108997760">NIKKI KNOCKERS</a>
http://profiles.friendster.com/108997760 - NIKKI KNOCKERS
<a href=http://profiles.friendster.com/108997760>NIKKI KNOCKERS</a>
http://profiles.friendster.com/108997760 - NIKKI KNOCKERS
<a href="http://profiles.friendster.com/108997925">NIKKI COX EXPOSED</a>
http://profiles.friendster.com/108997925 - NIKKI COX EXPOSED
<a href=http://profiles.friendster.com/108997925>NIKKI COX EXPOSED</a>
http://profiles.friendster.com/108997925 - NIKKI COX EXPOSED
<a href="http://profiles.friendster.com/108998090">PINK NUDE</a>
http://profiles.friendster.com/108998090 - PINK NUDE
<a href=http://profiles.friendster.com/108998090>PINK NUDE</a>
http://profiles.friendster.com/108998090 - PINK NUDE
<a href="http://profiles.friendster.com/108998266">LUCY PINDER NUDE</a>
http://profiles.friendster.com/108998266 - LUCY PINDER NUDE
<a href=http://profiles.friendster.com/108998266>LUCY PINDER NUDE</a>
http://profiles.friendster.com/108998266 - LUCY PINDER NUDE
By Paddy Power Bingo Free Bet at 8:09 p.m. on June 23, 2009
make the comment stick, thats what they say. but is yeltsin still here? maybe
By jordan shoes at 1:04 a.m. on June 25, 2009
doesnt work at my end :(
By jordan shoes at 1:04 a.m. on June 25, 2009
very nice and interesting post thank you !
By ugg boots at 1:05 a.m. on June 25, 2009
Thank you for the specific info you providing in the post.
By nike shoes at 1:05 a.m. on June 25, 2009
http://www.nikempire.com
By tiffany jewellery at 1:06 a.m. on June 25, 2009
Interesting insight
By nilson Jack at 12:44 a.m. on July 1, 2009
thanks post...
http://www.abercrombieonsale.com/
http://www.edhardyshop.us
http://www.edhardyuk.co.uk
http://www.lowratemortgagetoday.com/
By Stop Dreaming Start Action at 7:18 a.m. on July 2, 2009
I feel lucky can read this usefull news. Now I find something what i want to know..
Thank you for this great informations..
By Rusli Zainal Sang Visioner at 7:18 a.m. on July 2, 2009
i want to say very thank you for this great informations. now i understand about it.
thank you.