Standalone CGI Server - Question for Group

Sandy Norton sandskyfly at hotmail.com
Fri Mar 29 09:35:40 EST 2002


Jeremy wrote:
    
> I want to be able to do - have Python start a CGI server, serve a
> page, get form data, get data from a database, and display dynamic
> content.  I don't need anything industrial strength, just something
> that can handle a single user.


OK. This one has bugged me before even though it's actually quite simple:

    <tested on win2k py2.2>

[1] create the script that will instanciate CGIHTTPServer (server.py)
    # this is my favorite part (-:
    from CGIHTTPServer import test as serve
    if __name__=='__main__': serve()

[2] create an html file which is going to call your python cgi script (test.html)
    <html>
    <head>
        <title>CGI Test</title>
    </head>
    <body>
        <form name="myform" action="cgi-bin/testcgi.py" method="GET">
            <input type='text' name='query' size="40">
            <input type="submit" value=' search '>
        </form>
    </body>
    </html>


[3] create a folder in the same directory as the two files above (cgi-bin)

[4] create the script that will be called by your html file (testcgi.py)
    #!python
    
    import cgi   
    print "Content-Type: text/plain\n\n"
    form = cgi.FieldStorage()
    for name in form.keys():
        print "Input: " + name + " --> " + form[name].value + "\n"
    print "Finished!"

[5] point your browser to http://localhost:8000/test.html
    
    and enjoy.

ciao

Sandy



More information about the Python-list mailing list