Differences between revisions 1 and 5 (spanning 4 versions)
Revision 1 as of 2004-05-31 00:56:49
Size: 1107
Editor: dsl254-010-130
Comment: How to make a very simple web server.
Revision 5 as of 2004-07-19 08:12:15
Size: 3599
Editor: dsl254-010-130
Comment: x-ref CGI scripts
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
Scratch code, demonstrating a simple HTTP server: = BaseHTTPServer =

You can use this to make a simple HTTP web server.

[[TableOfContents()]]

== 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 ==

=== Responding with an HTML Page ===
Line 9: Line 22:
HOST_NAME = 'something.somewhere.net'
PORT_NUMBER = 80
HOST_NAME = 'something.somewhere.net' # !!!REMEMBER TO CHANGE THIS!!!
PORT_NUMBER = 80 # Maybe set this to 9000.
Line 23: Line 36:
        s.wfile.write( "<html><head><title>Title goes here.</title></head><body><p>This is a test.</p></body></html>" )         s.wfile.write( "<html><head><title>Title goes here.</title></head>" )
        s.wfile.write( "<body><p>This is a test.</p>" )
        # If someone went to "http://something.somewhere.net/foo/bar/",
        # then s.path equals "/foo/bar/".
        s.wfile.write( "<p>You accessed path: %s</p>" % s.path )
        s.wfile.write( "</body></html>" )
Line 38: Line 56:
See also: DocXmlRpcServer === Responding with URL Redirection ===

This code demonstrates simple URL redirection:

{{{
#!python
"""
URL redirection example.
"""

import BaseHTTPServer
import time
import sys


HOST_NAME = 'something.somewhere.net' # !!!REMEMBER TO CHANGE THIS!!!
PORT_NUMBER = 80 # Maybe set this to 9000.
REDIRECTIONS = {
    "/slashdot/" : "http://slashdot.org/",
    "/freshmeat/" : "http://freshmeat.net/",
    }
REDIRECTION_OF_LAST_RESORT = "http://google.com/"

class RedirectHandler( BaseHTTPServer.BaseHTTPRequestHandler ):
    def do_HEAD(s):
        s.send_response( 301 )
        s.send_header( "Location", REDIRECTIONS.get( s.path, REDIRECTION_OF_LAST_RESORT ) )
        s.end_headers()
    def do_GET(s):
        s.do_HEAD()

if __name__ == '__main__':
    server_class = BaseHTTPServer.HTTPServer
    httpd = server_class( (HOST_NAME, PORT_NUMBER),
                          RedirectHandler )
    print time.asctime(), "Server Starts - %s:%s" % (HOST_NAME, PORT_NUMBER)
    try:
        httpd.serve_forever()
    except KeyboardInterrupt:
        pass
    httpd.close()
    print time.asctime(), "Server Stops - %s:%s" % (HOST_NAME, PORT_NUMBER)
}}}

== See Also ==

 * DocXmlRpcServer -- self-documenting XML-RPC servers
 * CgiScripts -- using invoked CGI scripts, rather than running micro-web servers

= 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)]]

I'd also like to add code here showing how to service a POST request.

-- LionKimbro [[DateTime(2004-07-03T23:07:53Z)]]

BaseHTTPServer

You can use this to make a simple HTTP web server.

TableOfContents()

Official Documentation

Example Code

Responding with an HTML Page

   1 import time
   2 import BaseHTTPServer
   3 
   4 
   5 HOST_NAME = 'something.somewhere.net' # !!!REMEMBER TO CHANGE THIS!!!
   6 PORT_NUMBER = 80 # Maybe set this to 9000.
   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>" )
  20         s.wfile.write( "<body><p>This is a test.</p>" )
  21         # If someone went to "http://something.somewhere.net/foo/bar/",
  22         # then s.path equals "/foo/bar/".
  23         s.wfile.write( "<p>You accessed path: %s</p>" % s.path )
  24         s.wfile.write( "</body></html>" )
  25 
  26 if __name__ == '__main__':
  27     server_class = BaseHTTPServer.HTTPServer
  28     httpd = server_class( (HOST_NAME, PORT_NUMBER),
  29                           MyHandler )
  30     print time.asctime(), "Server Starts - %s:%s" % (HOST_NAME, PORT_NUMBER)
  31     try:
  32         httpd.serve_forever()
  33     except KeyboardInterrupt:
  34         pass
  35     httpd.close()
  36     print time.asctime(), "Server Stops - %s:%s" % (HOST_NAME, PORT_NUMBER)

Responding with URL Redirection

This code demonstrates simple URL redirection:

   1 """
   2 URL redirection example.
   3 """
   4 
   5 import BaseHTTPServer
   6 import time
   7 import sys
   8 
   9 
  10 HOST_NAME = 'something.somewhere.net' # !!!REMEMBER TO CHANGE THIS!!!
  11 PORT_NUMBER = 80 # Maybe set this to 9000.
  12 REDIRECTIONS = {
  13     "/slashdot/" : "http://slashdot.org/",
  14     "/freshmeat/" : "http://freshmeat.net/",
  15     }
  16 REDIRECTION_OF_LAST_RESORT = "http://google.com/"
  17 
  18 class RedirectHandler( BaseHTTPServer.BaseHTTPRequestHandler ):
  19     def do_HEAD(s):
  20         s.send_response( 301 )
  21         s.send_header( "Location", REDIRECTIONS.get( s.path, REDIRECTION_OF_LAST_RESORT ) )
  22         s.end_headers()
  23     def do_GET(s):
  24         s.do_HEAD()
  25 
  26 if __name__ == '__main__':
  27     server_class = BaseHTTPServer.HTTPServer
  28     httpd = server_class( (HOST_NAME, PORT_NUMBER),
  29                           RedirectHandler )
  30     print time.asctime(), "Server Starts - %s:%s" % (HOST_NAME, PORT_NUMBER)
  31     try:
  32         httpd.serve_forever()
  33     except KeyboardInterrupt:
  34         pass
  35     httpd.close()
  36     print time.asctime(), "Server Stops - %s:%s" % (HOST_NAME, PORT_NUMBER)

See Also

  • DocXmlRpcServer -- self-documenting XML-RPC servers

  • CgiScripts -- using invoked CGI scripts, rather than running micro-web servers

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)

I'd also like to add code here showing how to service a POST request.

-- LionKimbro DateTime(2004-07-03T23:07:53Z)

BaseHttpServer (last edited 2012-04-15 19:54:19 by PaulBoddie)

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