reinventing the web server

Thomas Güttler guettler at thomas-guettler.de
Wed Jun 18 08:26:56 EDT 2003


Simon Wittber (Maptek) wrote:

> Hello Python People.
> 
> I am very interested in using Python as a web development tool. I have
> investigated mod_python. It was rather difficult to setup, and seemed
> rather clunky.
> 
> I then checked out twisted web, which seemed to work quite well. However
> the rpy files left me cold. All I want is access to the session, form
> and querystrings and some server variables. I don't want all the twisted
> Resource() weirdness. Also, twisted seems to be a bit of a resource hog.
> A simple request was chewing up 10% of my poor Indy's CPU :(
> 
> Is it worthwhile reinventing my own Python web server, the way I want
> it? Or is it a waste of time... and should I be sticking to existing
> technologies?
> 
> Sw.

You can use CGIHTTPServer.
One script is listens on a port, the other script
gets executed (needs to be in a subdirectory called cgi-bin):

Webserver.py:
#!/usr/bin/env python
import sys
from CGIHTTPServer import CGIHTTPRequestHandler
import BaseHTTPServer

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


def run():
    httpd=BaseHTTPServer.HTTPServer(('', 8000), MyRequestHandler)
    httpd.serve_forever()

if __name__=="__main__":
    run()

test.py:
import cgitb; cgitb.enable()
import cgi
def header(title=""):
    ret="""<html><head><title>%s</title></head>
     <body>
     """ % title
    return ret

def footer():
    ret="""</body>
     </html>"""
    return ret

def main():
    ret=[]
    ret.append(header())
    ret.append("foo")
    ret.append(footer())
    return ''.join(ret)

if __name__=="__main__":
    ret=main()
    print "Content-Type: text/html"     # HTML is following
    print                               # blank line, end of headers
    print ret





More information about the Python-list mailing list