A different kind of Hello World
Following the last post, here’s a new Hello World:
The party responsible for the above:
# File: simplehttp.py import SocketServer PORT = 80 PAGE="""<html><head><title>Hello World!</title></head> <body>Hello World! You are reading a page served by a local server!<br/> Your request headers were: <br/> <pre>%s</pre></body></html>""" class HTTPRespHandler(SocketServer.StreamRequestHandler): def handle(self): print "connection from", self.client_address resp = "HTTP/1.0 200 OK\nContent-Type: text/html\n\n" + (PAGE % self.rfile.read(300)) self.wfile.write(resp) if __name__ == '__main__': server = SocketServer.TCPServer(("0.0.0.0", PORT), HTTPRespHandler) print "listening on port", PORT try: server.serve_forever() except: server.server_close()
Points of interest:
- The SimpleSocket module doesn’t ship with the Python for S60 distribution. But I just copied the
SimpleSocket.py
file from the standard Python 2.2 distribution to the phone and it worked as-is. - If I tried to
self.wfile.write(...)
in chunks instead of in one go, the browser would close the connection and time out. I could reproduce the same odd behaviour even with a simple, old-skoolsocket
based server. I don’t know what’s up with that.