06 Dec 05 - UPDATE: with this post I start a series of posts comparing scripts in .NET (C# or so) and Python (CPython)
Michele Bersani of the local .NET user group wrote a basic use case of .NET 2.0 HttpListener class but he states in his post that (I translate from italian) it works “only under Windows XP SP2 or Windows Server 2003“. What a pity!
Hence I wrote a Python similar version that runs on quite all platforms (Windows, Linux and so on).
- from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler
- import webbrowser
- class RequestHandler(BaseHTTPRequestHandler):
- def _writeheaders(self):
- self.send_response(200)
- self.send_header(‘Content-type’, ‘text/html’)
- self.end_headers()
- def do_HEAD(self):
- self._writeheaders()
- def do_GET(self):
- self._writeheaders()
- self.wfile.write(
- “”“<html><body><h1>This is the HTML body</h1></body></html>”“”)
- address = (‘localhost’, 8765)
- print “Connected on: %s:%s” % address
- server = HTTPServer(address, RequestHandler)
- webbrowser.browser = “iexplore.exe”
- webbrowser.open(“http://%s:%d” % address)
- server.serve_forever()
Just tweak the webbrowser part for make it work under all browsers.
06 Dec 05 - UPDATE: Valentino Volonghi wrote a version using the Twisted Matrix framework: A Web server in Twisted

