web.py with mod_python on Apache
Normally, I’m a Django-head, but today I wanted a quick way to write a small web app and have heard good things about web.py so I decided to give it a try. I also wanted minimum fuss to get something up-and-running quickly, and decided to have it served by my existing Apache installation with mod_python.
In the process of setting it up, I ran into a few bumps, and would like to share them:
- My setup is: Python 2.5.2, web.py 0.31, Apache 2.2.8 with mod_python 3.3.1
- The section on configuring mod_python on the web.py installation page doesn’t work for this version. In particular, it tells you to do this:
main = web.wsgifunc(web.webpyfunc(urls, globals()))
which seems to be the API in web.py 0.2. Using it on 0.3 will land you a nice
AttributeError: 'module' object has no attribute 'wsgifunc'. Instead, do this:app = web.application(urls, globals(), autoreload=False) main = app.wsgifunc()
It’s also worth noting that the version of modpython_gateway.py I’m using is revision 106. With this version, there is no need to place it in the
wsgirefpackage directory since it no longer depend onwsgiref. - My Apache config is:
Alias /path /vhosts/dready.org/apps/myapp/ <Directory /vhosts/dready.org/apps/myapp/> Allow from all <IfModule python_module> SetHandler python-program PythonHandler modpython_gateway::handler PythonOption wsgi.application codep::main PythonOption SCRIPT_NAME /path </IfModule> </Directory> - Notice that I specified the
autoreloadargument, because the other problem I ran into was that web.py complained the following:Traceback (most recent call last): File "/usr/local/lib/python2.5/site-packages/web.py-0.31-py2.5.egg/web/application.py", line 209, in process return p(lambda: process(processors)) File "/usr/local/lib/python2.5/site-packages/web.py-0.31-py2.5.egg/web/application.py", line 536, in processor h() File "/usr/local/lib/python2.5/site-packages/web.py-0.31-py2.5.egg/web/application.py", line 73, in reload_mapping mod = __import__(module_name) ImportError: No module named _mp_4ba3724007eff4c08a72054c4da2c222I’m not sure the reason for this, but definitely has something to do with web.py—s auto-reload feature, and the fact that it is running under
mod_pythonso the module name seems to be mangled. - The other thing is that if you ever run into the problem of getting a file save dialog when requesting the root URL of your app, with the content type of
httpd/unix-directory, but not other URLs, this could be due to either Apache ormod_pythonpassing the hint on the request, so if you don’t respond with aContent-Typeheader (usingweb.header('Content-Type', 'text/html'), that hint gets copied over to the response.
Related posts:
- Tornado with VirtualEnv and Pip Quickstart Friendfeed’s open source Tornado web server is great, and is incredibly easy to get up-and-running. Just install tornado, write your...
Related posts brought to you by Yet Another Related Posts Plugin.
May 16th, 2009 at 8:29 pm
测试
January 23rd, 2010 at 9:02 pm
I was having the "httpd/unix-directory" problem with web.py and Passenger (a WSGI thing). Adding web.header('Content-Type', 'text/html') inside the GET method solved the problem. I don't know how you analyzed the problem and found the solution, but thanks a lot for leaving a hint here.