Python CGI Scripting Documentation

Alex Martelli aleax at mac.com
Sun Jul 2 12:31:39 EDT 2006


Vlad Dogaru <ddvlad at gmail.com> wrote:

> Hello,
> 
> I would like to learn web scripting with Python (sure, everyone uses
> PHP, but I don't like the syntax and Python is more general-purpose
> and... well, I guess you people know the advantages better than me).
> Where can I get a thorough introduction to both CGI and using Python
> for CGI? That includes installing my own web server (at home, for
> testing) and starting from scratch (by that I mean I have near null
> experience with CGI).
> 
> I have tried looking through the source code of MoinMoin, but it's just
> too advanced for me -- I simply don't know where to start. Right now,
> I'll take any and all suggestions. However, please suggest books or
> articles that are up-to-date on Python programming; I'd hate to see
> that I'm studying obsolete texts or the like.

In terms of learning, Steve Holden's "Python Web Programming" is still
unbeatable -- it teaches you just enough of the many underlying
technologies, from HTTP to HTML to relational databases, as well as
Python.  However, it IS an old version of Python (sigh).  But it's very
easy to learn the relatively small enhancements to the Python language
since the time Steve penned his masterpiece... anything that used to
work then still works now, you have better ways to perform many tasks
(particularly thanks to additions to the standard library, and third
party extension modules matured in the meantime) but those are easy to
learn "afterwards" (and meanwhile, a solid understanding of the basics
of, say, HTTP and entity-relation design, will stand you in good stead
for years and years to come!-).  Steve's book is really the best you can
get, for "learning" purposes such as yours.  However...L

If you insist on getting coverage of the latest and greatest version of
Python, you might want to get the 2nd edition of my "Python in a
Nutshell", due out later this month; it strives to cover Python 2.5
(also due out later this month;-) as well as 2.4 (the still-now current
version, on which the new Nutshel focuses), and does have a chapter
specifically on CGI (not much changed from the first edition's, of
course, since CGI itself has not changed much over the years;-).

To have a look at my book (and just about any other O'Reilly book... and
not just O'Reilly either!) for free, subscribe to O'Reilly's "Safari"
online library -- I believe the first two weeks are free, so, as long as
you cancel in time, you should not have to pay a penny for the
privilege; thus, it may be a good idea (I personally like having access
to said library for searches etc, but that's a separate issue).  It will
be a while before the 2nd edition gets online -- but as I said the CGI
part is basically unchanged anyway.

But, talking of web resources...:

A Google search for Python CGI gives you 20 million hits, and quite a
few of those appear to be good tutorials on the subject -- why don't you
give those a try, first?  Sure, most of the pages will be using older
versions of Python -- but most of the differences should be of little
importance. One crucial one: you *DO* want to use the cgitb auxiliary
module to get good tracebacks in case of errors, and older sites may not
mention it -- however, that's pretty simple indeed...:

  import cgitb; cgitb.enable()

just place this line at the very top of your CGI script (after the
"shebang" line and the docstring, heh:-) and you're all set.  Any
further info you need for "advanced" usage of cgitb is in one tiny page
at <http://docs.python.org/lib/module-cgitb.html>.


Alex



More information about the Python-list mailing list