[Tutor] Database driven web sites with python

Danny Yoo dyoo@hkn.eecs.berkeley.edu
Thu Jan 23 11:49:01 2003


On Thu, 23 Jan 2003, Paul Hartley wrote:

> I tried the following code from a book - I just wanted a simple web site
> to test out some ideas and balked at installing Apache or Zope on my PC.

Hi Paul!

Having a Web server on a PC may not be too bad of an idea though, since
it'll allow you to play around with web serving.  Since Apache's free for
download, why not try it out?

    http://httpd.apache.org/
    http://httpd.apache.org/docs-2.0/howto/cgi.html

You may find that it's not as difficult as you might expect.  And if you
do run into problems, Apache has a few resources that you can try out:

    http://httpd.apache.org/lists.html#http-users





> I would like to know how to get cgi scripts to work with this code (what
> do I put in the htmp page etc. simple steps!!

No problem.  Let's take a look.



> # web.py
>
> import os
>
> from BaseHTTPServer import HTTPServer
>
> from CGIHTTPServer import CGIHTTPRequestHandler

Ok, good, so this gives us access to the CGIHTTPServer class.



> # os.chdir("/test/web/html")  # Change to web directory!
>
> srvraddr = ("", 80)
> srvrobj = HTTPServer(srvraddr, CGIHTTPRequestHandler)
>
> srvrobj.serve_forever()

(You may run into a small permissions problem here: port 80, the port for
http web serving, should be reserved for administrative users.  On Unix
systems, any port below 1024 can be grabbed by a program only by root, for
security reasons.  So while you're testing CGI stuff, you might need to
run your program on a different port, like '8080'.  This warning might not
apply on Windows systems though...)


Ah!  In your line here, when you create the server itself:

> srvrobj = HTTPServer(srvraddr, CGIHTTPRequestHandler)

you may need to customize the CGIHTTPRequestHandler: by default, it looks
for cgi's the '/cgi-bin' and '/htbin' directories, but you may need to
extend this if you want CGI's to run in different places.

For example, here's a way to customize that class so that whenever a url
begins with '/mycgis', Python will treat scripts there as CGI's:

###
class CustomCgiHandler(CGIHTTPRequestHandler):
    cgi_directories = ["/mycgis"]
###



Once we have something like this, we can feed this CustomCgiHandler class
to our server.


Here's an interactive session that shows how this can work:

###
[dyoo@tesuque dyoo]$ pwd
/home/dyoo
[dyoo@tesuque dyoo]$ ls cgi-bin
hello.py
[dyoo@tesuque dyoo]$ cat cgi-bin/hello.py
#!/usr/bin/env python

print "Content-type: text/plain\n\n"
print "Hello world!"


[dyoo@tesuque dyoo]$ cat test_cgi.py
from BaseHTTPServer import HTTPServer
from CGIHTTPServer import CGIHTTPRequestHandler

class CustomCgiHandler(CGIHTTPRequestHandler):
    cgi_directories = ["/cgi-bin"]


def main():
    srvraddr = ("", 8888)
    srvrobj = HTTPServer(srvraddr, CustomCgiHandler)
    srvrobj.serve_forever()



if __name__ == '__main__':
    main()
[dyoo@tesuque dyoo]$ python test_cgi.py &
[1] 15919
[dyoo@tesuque dyoo]$ lynx -source http://localhost:8888/cgi-bin/hello.py
localhost.localdomain - - [23/Jan/2003 08:44:19] "GET /cgi-bin/hello.py
HTTP/1.0" 200 -

Hello world!
###


That last command using 'lynx' is a Unix program that grabs web resources
and dumps them to screen., and the few lines about 'localhost.localdomain'
is log information that the server prints out on every request.  But at
the very bottom, we see the glorious message 'Hello world!", so we know
that things are working.


Does this work for you?  I hope this helps!