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 wsgiref package directory since it no longer depend on wsgiref.

  • 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 autoreload argument, 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_4ba3724007eff4c08a72054c4da2c222
    

    I’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_python so 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 or mod_python passing the hint on the request, so if you don’t respond with a Content-Type header (using web.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.


5 Responses to “web.py with mod_python on Apache”

  1. test Says:

    测试

  2. Progga Says:

    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.

  3. ayaz Says:

    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

  4. @parkchiwan Says:

    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.

  5. wil Says:

    @parkchiwan – good to know it helped you :)