Differences between revisions 8 and 9
Revision 8 as of 2005-02-02 22:54:51
Size: 1757
Editor: anonymous
Comment: missing edit-log entry for this revision
Revision 9 as of 2011-02-15 06:05:18
Size: 1757
Editor: localhost
Comment: converted to 1.6 markup
No differences found!

You can run the same Albatross script as a CGI or from mod_python, or even from the command line, with the following trick:

        from albatross import SimpleSessionFileApp, SessionFileAppContext
        try:
            from albatross.apacheapp import Request # This will fail if we are not inside mod_python
            cgi = 0
        except ImportError:
            from albatross.cgiapp import Request
            cgi = 1

        # Page classes, application/context objects, etc here

        app = App()

        def handler(req):
           '''Called from mod_python - turn a mod_python req into an Albatross Request'''
            return do_handler(Request(req))

        def do_handler(r):
           '''Called with an Albatross Request object'''
            return app.run(r)

        if cgi:
            do_handler(Request())


It's actually even easier than that:

app = App()
if __name__ == '__main__':
    # CGI
    from albatross import cgiapp
    app.run(cgiapp.Request())
else:
    # mod_python
    from albatross import apacheapp
    def handler(req):
        return app.run(apacheapp.Request(req))

But which do people prefer?

-- Matt


The fastcgi support (available in 1.10) will work for both normal cgi and fastcgi so you can actually support cgi, fastcgi and mod_python all at the same time. -- Matt


The smaller version will not work with your standalone server.


Matt's point is good, but in our case, we need the cgi variable for other things, in particular for working out where to find the templates for this application, and that has to be known when building the App object. That's why we use the split approach.

-- Greg

None: CGI_and_mod_python (last edited 2011-02-15 06:05:18 by localhost)