views:

5795

answers:

4

Hello,

I have a Bluehost account where I can run Python scripts as CGI. I guess it's the simplest CGI because to run I have to define the following in .htaccess:

Options +ExecCGI
AddType text/html py
AddHandler cgi-script .py

Now, whenever I look up web programming with Python, I hear a lot about WSGI and how most frameworks use it. But I just don't understand how it all fits together, especially when my web server is given (Apache running at a host's machine) and not something I can really play with (except defining .htaccess commands).

Can someone please shed some light on how WSGI, CGI, and the frameworks are all connected ? What do I need to know / install / do if I want to run a web framework (say web.py or cherrypy) on my basic CGI configuration ? How to install WSGI support ?

Thanks in advance

+11  A: 

You can run wsgi over cgi as Pep333 demonstrates as an example. However everytime there is a request a new python interpreter is started and the whole context (database connections etc.) needs to be build which all take time.

The best if you want to run wsgi would be if your host would install mod_wsgi and made an appropriate configuration to defer control to an application of yours.

Flup is another way to run with wsgi for any webserver that can speak FCGI, SCGI or AJP. From my experience only FCGI really works, and it can be used in apache either via mod_fastcgi or if you can run a seperate python daemon with mod_proxy_fcgi

Wsgi is a protocol much like CGI, which defines a set of rules how webserver and python code can interact, it is defined as Pep333. It makes it possible that many different webservers can use many different frameworks and applications using the same application protocol. This is very beneficial and makes it so useful.

Florian Bösch
Is running WSGI over CGI what "flup" is for ? How is flup connected to the scheme ?
Eli Bendersky
+8  A: 

I think Florian's answer answers the part of your question about "what is WSGI", especially if you read the PEP.

As for the questions you pose towards the end:

WSGI, CGI, FastCGI etc. are all protocols for a web server to run code, and deliver the dynamic content that is produced. Compare this to static web serving, where a plain HTML file is basically delivered as is to the client.

CGI, FastCGI and SCGI are language agnostic. You can write CGI scripts in Perl, Python, C, bash, whatever. CGI defines which executable will be called, based on the URL, and how it will be called: the arguments and environment. It also defines how the return value should be passed back to the web server once your executable is finished. The variations are basically optimisations to be able to handle more requests, reduce latency and so on; the basic concept is the same.

The difference with WSGI is that it's Python only. Rather than a language agnostic protocol, a standard function signature is defined:

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']

That is a complete (if limited) WSGI application. A web server with WSGI support (such as Apache with mod_wsgi) can invoke this function whenever a request arrives.

The reason this is so great is that we can avoid the messy step of converting from a HTTP GET/POST to CGI to Python, and back again on the way out. It's a much more direct, clean and efficient linkage.

It also makes it much easier to have long-running frameworks running behind web servers, if all that needs to be done for a request is a function call. With plain CGI, you'd have to start your whole framework up for each individual request.

To have WSGI support, you'll need to have installed a WSGI module (like mod_wsgi), or use a web server with WSGI baked in (like CherryPy). If neither of those are possible, you could use the CGI-WSGI bridge given in the PEP.

Alabaster Codify
+1  A: 

It's a simple abstraction layer for Python, akin to what the Servlet spec is for Java. Whereas CGI is really low level and just dumps stuff into the process environment and standard in/out, the above two specs model the http request and response as constructs in the language. My impression however is that in Python folks have not quite settled on de-facto implementations so you have a mix of reference implementations, and other utility-type libraries that provide other things along with WSGI support (e.g. Paste). Of course I could be wrong, I'm a newcomer to Python. The "web scripting" community is coming at the problem from a different direction (shared hosting, CGI legacy, privilege separation concerns) than Java folks had the luxury of starting with (running a single enterprise container in a dedicated environment against statically compiled and deployed code).

aaron
+28  A: 

How WSGI, CGI, and the frameworks are all connected ?

Apache listens on port 80. It gets an HTTP request. It parses the request to find a way to respond. Apache has a LOT of choices for responding. One way to respond is to use CGI to run a script. Another way to respond is to simply serve a file.

In the case of CGI, Apache prepares an environment and invokes the script through the CGI protocol. This is a standard Linux Fork/Exec situation -- the CGI subprocess inherits an OS environment including the socket and stdout. The CGI subprocess writes a response, which goes back to Apache; Apache sends this respond to the browser.

CGI is primitive and annoying. Mostly because it forks a subprocess for every request.

WSGI is an interface that is based on the CGI design pattern. It is not necessarily CGI -- it does not have to fork a subprocess for each request. It can be CGI, but it doesn't have to be.

WSGI adds to the CGI design pattern in several important ways. It parses the HTML headers for you and adds these to the environment. It supplies any POST-oriented input as a file-like object in the environment. It also provides you a function that will formulate the response, saving your from a lot of formatting details.

What do I need to know / install / do if I want to run a web framework (say web.py or cherrypy) on my basic CGI configuration ?

Recall that forking a subprocess is expensive. There are two ways to work around this.

  1. Embedded mod_wsgi or mod_python embeds Python inside Apache; no process is forked. Apache runs the Django application directly.

  2. Daemon mod_wsgi or mod_fastcgi allows Apache to interact with a separate daemon (or "long-running process"), using the WSGI protocol. You start your long-running Django process, you configure Apache's mod_fastcgi to communicate with this process.

Note that mod_wsgi can work in either mode: embedded or daemon.

When you read up on mod_fastcgi, you'll see that Django uses on flup to create a WSGI-compatible interface from the information provided by mod_fastcgi. The pipeline works like this.

Apache -> mod_fastcgi -> FLUP (via CGI protocol) -> Django (via WSGI protocol)

Django has several "django.core.handlers" for the various interfaces.

For mod_fastcgi, Django provides a manage.py runfcgi that integrates FLUP and the handler.

For mod_wsgi, there's a core handler for this.

How to install WSGI support ?

Follow these instructions.

http://code.google.com/p/modwsgi/wiki/IntegrationWithDjango

For background see this

http://docs.djangoproject.com/en/dev/howto/deployment/#howto-deployment-index

S.Lott
I can't install mod_wsgi because i'm on shared hosting. All I have is fcgi support. How can I still run WSGI apps through it?
Eli Bendersky
thanks for the details
Eli Bendersky
claws
@claws: Please do not ask which is "better". The question is silly. Better at what? Cheaper? Faster? Uses less memory? Involves the most programming? Almost everyone uses mod_wsgi because it does everything.
S.Lott
S.Lott, instead of complaining about when people ask which is "better", why not simply state "mod_wsgi is better as X, fastcgi is better for Y", and if the OP has more specific questions, they'll ask.
Gregg Lind
@Greg Lind: Why not simply state "mod_wsgi is better as X, fastcgi is better for Y"? Because it's not very easy to do. There are dozens of non-functional quality factors that are elements of the X and Y sets. It's difficult to enumerate them all. It's far, far better for folks to ask specific questions about the quality factors that are relevant.
S.Lott