CGI docs

Skip Montanaro skip at pobox.com
Fri Oct 25 11:51:40 EDT 2002


    Catalin> For example, I have the following link in a webpage:

    Catalin> <a href = "http://localhost/mhpp/hello.py?name='Catalin'">name</a>

    Catalin> I want my script "hello.py" to be able to pick up the
    Catalin> "name:Catalin" pair. The closest I came to finding a way to do
    Catalin> it is the "urlparse" function described, among other places, at
    Catalin> "http://www.python.org/doc/current/lib/module-urlparse.html".

The cgi module is what you want.  Look at

    http://www.python.org/doc/current/lib/node297.html

which is a subnode of the CGI module docs entitled "Using the cgi module".
It contains an example which goes something like

    form = cgi.FieldStorage()
    if not (form.has_key("name"):
        print "<h1>Error</h1>"
        print "No name was given."
        return

    name = form["name"].value
    ...

Attached is a simple CGI script which displays information about the
environment in which it was run and the parameters passed in the URL or form
submission.  It uses the cgi module to parse the input parameters.  To use
it, drop it in your cgi-bin directory as query.py and invoke it like so:

    http://www.myserver.com/cgi-bin/query
    http://www.myserver.com/cgi-bin/query?name=Catalin
    http://www.myserver.com/cgi-bin/query?name=Catalin&country=no

If you don't have a version of Python which comes with the cgitb module,
simply comment out the two lines involving it at the start of the script.
They are only there as a convenience.

-- 
Skip Montanaro - skip at pobox.com
http://www.mojam.com/
http://www.musi-cal.com/


-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/octet-stream
Size: 5107 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20021025/74c020e4/attachment.obj>


More information about the Python-list mailing list