inicio mail me! sindicaci;ón

HttpListener (.NET) vs BaseHTTPServer (Python)

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).

  1. from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler
  2. import webbrowser
  3.  
  4. class RequestHandler(BaseHTTPRequestHandler):
  5.     def _writeheaders(self):
  6.         self.send_response(200)
  7.         self.send_header(‘Content-type’, ‘text/html’)
  8.         self.end_headers()
  9.  
  10.     def do_HEAD(self):
  11.         self._writeheaders()
  12.  
  13.     def do_GET(self):
  14.         self._writeheaders()
  15.         self.wfile.write(
  16.             “”“<html><body><h1>This is the HTML body</h1></body></html>”“”)
  17.        
  18. address = (‘localhost’, 8765)
  19. print “Connected on: %s:%s” % address
  20. server = HTTPServer(address, RequestHandler)
  21.  
  22. webbrowser.browser = “iexplore.exe”
  23. webbrowser.open(“http://%s:%d” % address)
  24.  
  25. 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

Related posts

  • Implementing Python with .NET
  • Thinking about the future (and mine too)
  • Ready to roll on the summer of code
  • Updates from Python SVN, Part 12
  • On the ride again!
  • Gravatar

    PDI^2 :: Post Archivio n. 3 :: December :: 2005 said,

    December 6, 2005 @ 3:50 pm

    […] valentino e lawrence si divertono a fare webserver confrontando con un originale in C# […]

    Gravatar

    PDI^2 :: E’ venerdi sera e io sono un geek del cavolo :: December :: 2005 said,

    December 10, 2005 @ 1:16 am

    […] Colgo l’occasione per giocare un po’ con rhymes, perché sono io che l’ho aizzato a fare certe cose.. […]

    RSS feed for comments on this post · TrackBack URI

    Leave a Comment