Revision 1 as of 2004-05-31 00:56:49

Clear message

Scratch code, demonstrating a simple HTTP server:

   1 import time
   2 import BaseHTTPServer
   3 
   4 
   5 HOST_NAME = 'something.somewhere.net'
   6 PORT_NUMBER = 80
   7 
   8 
   9 class MyHandler( BaseHTTPServer.BaseHTTPRequestHandler ):
  10     def do_HEAD(s):
  11         s.send_response(200)
  12         s.send_header("Content-type", "text/html")
  13         s.end_headers()
  14     def do_GET(s):
  15         """Respond to a GET request."""
  16         s.send_response(200)
  17         s.send_header("Content-type", "text/html")
  18         s.end_headers()
  19         s.wfile.write( "<html><head><title>Title goes here.</title></head><body><p>This is a test.</p></body></html>" )
  20 
  21 if __name__ == '__main__':
  22     server_class = BaseHTTPServer.HTTPServer
  23     httpd = server_class( (HOST_NAME, PORT_NUMBER),
  24                           MyHandler )
  25     print time.asctime(), "Server Starts - %s:%s" % (HOST_NAME, PORT_NUMBER)
  26     try:
  27         httpd.serve_forever()
  28     except KeyboardInterrupt:
  29         pass
  30     httpd.close()
  31     print time.asctime(), "Server Stops - %s:%s" % (HOST_NAME, PORT_NUMBER)

See also: DocXmlRpcServer

Unable to edit the page? See the FrontPage for instructions.