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.
No related posts.
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.
December 9th, 2010 at 3:48 am
Hey Wil:
This was a great post. Thank you.
I am trying to set up a small web.py application on mod_python where web.py is installed under a virtual environment. But I am getting ImportErrors. I've posted my question over here: http://stackoverflow.com/questions/4384946/mod-py...
I am wondering if you could please take a look and share any thoughts about it. I would really appreciate that. Thank you.
Ayaz
January 26th, 2011 at 3:39 am
Dear Wil.
WOW. This is very great post.
I had a same problem in my frihost account.
I didn't find the solution before I read this post.
I would really appreciate this post and you. Thank you.
January 26th, 2011 at 10:21 pm
@parkchiwan – good to know it helped you