Python for Web Applications

From Wildsong
Revision as of 21:04, 11 September 2013 by Brian Wilson (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search

Apache

With Apache, use mod_wsgi not mod_python

apt-get remove libapache2-mod-python
apt-get install libapache2-mod-wsgi

On Ubuntu this gives me version 3.4 which is quite current.

GCM

I am writing a web application to work with Google Cloud Messaging (GCM).

Currently I am working with the version of GCM that flies over HTTP. Later I will look at the XMPP version.

GCM allows me to send a message to any number of connected Android devices.

To send to a given device, the device has to register with my server. It does this with an HTTP POST. It comes in on an URL that is coded into the Android app and it could look like this:

http://myserver/gcm-demo/register?regId=some long ugly string

but it's a POST not REST so the regId and the string come in as data in the POST. I can test with the REST version though.

but this page is about writing a

Web application

I can whip up bits of Python and run them on my Apache server via mod_python. Great. But what I _really_ need to be able to do is DEBUG them in Komodo.

I can get some abstraction by using cherrypy, so that the code is more portable instead of locked to mod_python.

Here's a page about using Wing in Stackoverflow Is it possible to debug cherrypy applications

So how about if I write my code using cherrypy as the framework? Can I debug? A resounding 'YES.

See also HTTPREPL which is interesting. Code and test in a browser!

Minimal web app

Start up your IDE (Komodo for me, presumably Wing is the same). Load up this code sample. Put a breakpoint at the 'return msg' line. Run.

In a browser, open up http://localhost:8080/ The debugger should then hit the breakpoint, you can examine what is going on, and then hit 'Resume' (F5 key). A page will be returned to the browser with the current time in it. We are on our way.

#!/usr/bin/env python

import time
import cherrypy

# Redirect my log messages to a file, even when running standalone.
cherrypy.config['log.error_file'] = '/var/www/python-test/logs/py_error.log'

class MyCherryApp:
    def index(self):
        msg = "<h1>Greetings</h1>"
        msg += time.asctime()
        return msg
    index.exposed = True

# This is the line of code that makes the application runnable from Apache via wsgi.
# WSGI looks for a function called 'application'.
application = cherrypy.Application(MyCherryApp(), script_name=None, config=None)

# This is the code that makes the application run standalone,
# so you can run in a debugger or run it standalone.
if __name__ == '__main__':
    
    #cherrypy.config.update()
    cherrypy.tree.mount(MyCherryApp())

    # Run the web server
    engine = cherrypy.engine
    try:
        engine.start()
    except:
        sys.exit(1)
    else:
        engine.block()

    print "I am all done."

What about Apache?

Can I run this sample directly in Apache too? That's the goal

I initially tried mod_python, which seems obvious but it's actually outdated. The cool kids all use mod_wsgi now.

First digest mod_wsgi then refer to Running CherryPy behind Apache using Mod_WSGI

Mode: embedded or daemon? The mod_wsgi page favours daemon mode so that is what I am trying first. Here is the Quick Configuration Guide.

Embedded mode: Enabling a script is one line in the Apache configuration, for example this creates the URL http://localhost/wsgi-test and connects it to the script wsgi-test.py

WSGIScriptAlias /wsgi-test /var/www/python-tests/wsgi-test.py

Daemon mode: add more lines above the script alias line.

# Use a real host name for a real server...
WSGIDaemonProcess localhost processes=2 threads=15
WSGIProcessGroup localhost
WSGIScriptAlias /wsgi-test /var/www/python-tests/wsgi-test.py