BaseHTTPServer
You can use this to make a simple HTTP web server.
Official Documentation
[http://www.python.org/doc/2.3.4/lib/module-BaseHTTPServer.html BaseHTTPServer module documentation] - what we use directly
[http://www.python.org/doc/2.3.4/lib/module-SocketServer.html SocketServer module documentation] - behind the BaseHttpServer
Example Code
Toggle line numbers
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
Discussion
I'd ultimately like to see a BaseHttpServer here that can both handle XML-RPC requests (with that request handler,) and normal web requests (with a custom handler.)
Yes- I know and love TwistedPython. But I want to make something that works in a single install. -- LionKimbro DateTime(2004-05-31T01:13:16Z)