running code from "Scripting The Web with Python"

Hamish Lawson hamish_lawson at yahoo.co.uk
Tue Mar 27 05:38:55 EST 2001


Eugene Leitl wrote:

> I've snipped the following code from Guido's
> http://www.w3j.com/6/s3.vanrossum.html presentation:
> ... I can't run it due to syntax errors


Wow, they really messed up the formatting on that! Below is the
properly formatted version.

Hamish Lawson

---

import StringIO
import SimpleHTTPServer

BaseClass = SimpleHTTPServer.SimpleHTTPRequestHandler

CounterTemplate = """ <H1>Server
Statistics</H1>

This <A HREF=/index.html>server</A> has been accessed
<b>%d</b> times.  """

count = 0

class MyRequestHandler(BaseClass):

    def do_GET(self):
        global count
        count = count + 1
        BaseClass.do_GET(self)

    def send_head(self):
        if self.path == "/counter.html":
            return self.send_counter()
        else:
            return BaseClass.send_head(self)

    def send_counter(self):
        self.send_response(200)
        self.send_header("Content-type", "text/html")
        self.end_headers()
        text = CounterTemplate % count
        return StringIO.StringIO(text)

def test():
    SimpleHTTPServer.test(MyRequestHandler)

test() 


____________________________________________________________
Do You Yahoo!?
Get your free @yahoo.co.uk address at http://mail.yahoo.co.uk
or your free @yahoo.ie address at http://mail.yahoo.ie




More information about the Python-list mailing list