WSGI - How Does It Affect Me?

Damjan gdamjan at gmail.com
Sun Oct 8 23:47:24 CEST 2006


> So I keep hearing more and more about this WSGI stuff, and honestly I
> still don't understand what it is exactly and how it differs from CGI
> in the fundamentals (Trying to research this on the web now)
> 
> What I'm most confused about is how it affects me.  I've been writing
> small CGI programs in Python for a while now whenever I have a need
> for a web program.  Is CGI now considered "Bad"?  

Well, mostly "yes" :)

> I've just always 
> found it easier to write something quickly with the CGI library than
> to learn a framework and fool with installing it and making sure my
> web host supports it.
> 
> Should I switch from CGI to WSGI?  What does that even mean?  What is
> the equivalent of a quick CGI script in WSGI, or do I have to use a
> framework even for that?  What do I do if frameworks don't meet my
> needs and I don't have a desire to program my own?

def simple_app(environ, start_response):
    """Simplest possible application object""" 
    status = '200 OK'
    response_headers = [('Content-type','text/plain')]
    start_response(status, response_headers)
    return ['Hello world!\n']

To serve it as a CGI just:
from wsgiref.handlers import CGIHandler
CGIHandler().run(simple_app)

It's not that complicated isn't it... and later you might want to move to
mod_python, scgi or fastcgi or IIS... you will not have to modify
simple_app a bit.

OR... you might want to use the EvalException middleware... just wrap your
simple_app like this:
app = EvalException(simple_app)

(well, due to it's simplicity EvalException can only work in single-process,
long running WSGI servers like not in CGI) so:

s = wsgiref.simple_server.make_server('',8080, app)
s.server_forever()

More info at
http://wsgi.org/wsgi/Learn_WSGI

> 3. Using IIS at all for that matter, does WSGI work on IIS, do any
> frameworks?

There's an IIS server gateway (WSGI server) but you can always run WSGI
applications with CGI, as seen above.



-- 
damjan



More information about the Python-list mailing list