Server-side Applications - examples wanted

Alex Martelli alex at magenta.com
Sat Aug 26 15:54:43 EDT 2000


<timin at homeSPAMNOT.com> wrote in message news:39A7FDB6.67DB31AC at home.com...
> The simpler the better.  Skeleton examples would be best.  Something that
I
> can run in my linux box that someone else can connect to, and that does
> something more than straight HTML can do.  (and primarily or totally in
> Python, of course!)

Simplest, on Linux (or other Unix variants), is to run the CGIHTTPServer
that
comes with Python; see 11.16 in the library reference.  When some page
is obtained from directory /cgi-bin, this server will treat it as a CGI
script.

To write the CGI script in Python, see 11.2.2 in the Library Reference --
how to use the CGI module (which also comes with Python).  There are
a few example snippets on that documentation page, but here's the
simplest example one can imagine:

place this script as foo.py in the cgi-bin subdirectory of whatever
the 'current directory' it where you start the cgi-http-server from
(that 'current directory' acts as the root of your webspace):

#! /usr/env python -u

import cgi
cgi.test()

and mark it as executable with +x.

Now, anybody visiting page /cgi-bin/foo.py on your server will
get, as a response, the output of cgi.test(), which summarizes
all the environment settings and form-fields used to visit it.


CGI, the Common Gateway Interface, runs just about the same
under any webserver, including the widespread Apache and the
little, simple, lightweight Xitami (www.imatix.com).  There are
other, more advanced protocols (LRWP under Xitami is neat;
FastCGI under Apache and others is, too; and Apache is famous
for the number of non-CGI ways you can connect Python to it,
through modules PyApache, mod_python, mod_snake...).  But
CGI is probably OK for most tasks -- you can also use cookies
to help maintain the illusion of a continuing session, etc.


Alex






More information about the Python-list mailing list