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:
- mod_python OpenID Access Control Since XRI is pretty much in bed with OpenID and NeuStar is an XRI shop, I get to play around...
- Memcached on Solaris If you observe memcached exhibiting strange behavior while running under Solaris, you should try upgrading to the latest version of...
- 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...
- Python 3.0: Text vs. Data Instead Of Unicode vs. 8-bit Python 3.0 (Py3K) is out. I’m with Sam Ruby — this seemingly simple change of paradigm from “Unicode vs. 8-bit”...
- Phinger Me Login: wil Name: William Tan Directory: /home/wil Shell: /usr/local/bin/bash Last seen offline: ??? Too many junk mails. .plan: EOF...
Related posts brought to you by Yet Another Related Posts Plugin.