Differences between revisions 4 and 16 (spanning 12 versions)
Revision 4 as of 2004-07-03 23:07:53
Size: 3477
Editor: dsl254-010-130
Comment: +How to redirect a web browser to another URL.
Revision 16 as of 2012-04-15 19:54:19
Size: 4693
Editor: PaulBoddie
Comment: Revert vandalism.
Deletions are marked like this. Additions are marked like this.
Line 5: Line 5:
[[TableOfContents()]] <<TableOfContents>>
Line 9: Line 9:
 * [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
 * [[http://docs.python.org/lib/module-BaseHTTPServer.html|BaseHTTPServer module documentation]] - what we use directly
 * [[http://docs.python.org/lib/module-SocketServer.html|SocketServer module documentation]] - behind the BaseHttpServer
Line 22: Line 22:
HOST_NAME = 'something.somewhere.net' # !!!REMEMBER TO CHANGE THIS!!! HOST_NAME = 'example.net' # !!!REMEMBER TO CHANGE THIS!!!
Line 26: Line 26:
class MyHandler( BaseHTTPServer.BaseHTTPRequestHandler ): class MyHandler(BaseHTTPServer.BaseHTTPRequestHandler):
Line 36: Line 36:
        s.wfile.write( "<html><head><title>Title goes here.</title></head>" )
        s.wfile.write( "<body><p>This is a test.</p>" )
        s.wfile.write("<html><head><title>Title goes here.</title></head>")
        s.wfile.write("<body><p>This is a test.</p>")
Line 40: Line 40:
        s.wfile.write( "<p>You accessed path: %s</p>" % s.path )
        s.wfile.write( "</body></html>" )
        s.wfile.write("<p>You accessed path: %s</p>" % s.path)
        s.wfile.write("</body></html>")
Line 45: Line 45:
    httpd = server_class( (HOST_NAME, PORT_NUMBER),
                         
MyHandler )
    httpd = server_class((HOST_NAME, PORT_NUMBER), MyHandler)
Line 52: Line 51:
    httpd.close()     httpd.server_close()
Line 71: Line 70:
HOST_NAME = 'something.somewhere.net' # !!!REMEMBER TO CHANGE THIS!!! HOST_NAME = 'example.net' # !!!REMEMBER TO CHANGE THIS!!!
Line 73: Line 72:
REDIRECTIONS = {
    
"/slashdot/" : "http://slashdot.org/",
    "/freshmeat/" : "http://freshmeat.net/",
    
}
REDIRECTION_OF_LAST_RESORT = "http://google.com/"
REDIRECTIONS = {"/slashdot/": "http://slashdot.org/",
                "/freshmeat/": "http://freshmeat.net/"}
LAST_RESORT = "http://google.com/"
Line 79: Line 76:
class RedirectHandler( BaseHTTPServer.BaseHTTPRequestHandler ): class RedirectHandler(BaseHTTPServer.BaseHTTPRequestHandler):
Line 81: Line 78:
        s.send_response( 301 )
        s.send_header( "Location", REDIRECTIONS.get( s.path, REDIRECTION_OF_LAST_RESORT ) )
        s.send_response(301)
        s.send_header("Location", REDIRECTIONS.get(s.path, LAST_RESORT))
Line 89: Line 86:
    httpd = server_class( (HOST_NAME, PORT_NUMBER),
                         
RedirectHandler )
    httpd = server_class((HOST_NAME, PORT_NUMBER), RedirectHandler)
Line 96: Line 92:
    httpd.close()     httpd.server_close()
Line 100: Line 96:
== Parameterizing ==

When you want to make your server respect some parameters, it's easiest to do so via the ''server,'' rather than the ''handler.''

That is, make a subclass of the server, that accepts some additional parameters. Have the server record those parameters.

Then, in the handler, use {{{s.server}}} to get to the server, and access the parameters through it.
Line 102: Line 106:
DocXmlRpcServer  * DocXmlRpcServer -- self-documenting XML-RPC servers
 * CgiScripts -- using invoked CGI scripts, rather than running micro-web servers
Line 108: Line 113:
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)]] 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)>>
Line 112: Line 117:
-- LionKimbro [[DateTime(2004-07-03T23:07:53Z)]] -- LionKimbro <<DateTime(2004-07-03T23:07:53Z)>>

There exist tools like CherryPy which will create a single-file Python HTTP server (based on BaseHTTPServer). This is a fair amount easier to work with than the raw BaseHTTPServer. For most cases, using a more complete framework will be preferable (see WebProgramming). -- IanBicking

I like the BaseHttpServer because it is in the default Python distributions. I encourage all work towards putting a standard web framework into the default Python distribution. I'm not picky, just as long as something is chosen. -- LionKimbro <<DateTime(2005-01-25T04:53:53Z)>>

What's the matter with server_close()? I can call the method, but it is undocumented (see [[http://docs.python.org/lib/node634.html]]). Could someone knowledgable either remove the calls, or add a comment why they're necessary? Thanks. -- Anonymous Coward, 23 Oct 2007

BaseHTTPServer

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

Official Documentation

Example Code

Responding with an HTML Page

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

Parameterizing

When you want to make your server respect some parameters, it's easiest to do so via the server, rather than the handler.

That is, make a subclass of the server, that accepts some additional parameters. Have the server record those parameters.

Then, in the handler, use s.server to get to the server, and access the parameters through it.

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 2004-05-31 01:13:16

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

-- LionKimbro 2004-07-03 23:07:53

There exist tools like CherryPy which will create a single-file Python HTTP server (based on BaseHTTPServer). This is a fair amount easier to work with than the raw BaseHTTPServer. For most cases, using a more complete framework will be preferable (see WebProgramming). -- IanBicking

I like the BaseHttpServer because it is in the default Python distributions. I encourage all work towards putting a standard web framework into the default Python distribution. I'm not picky, just as long as something is chosen. -- LionKimbro 2005-01-25 04:53:53

What's the matter with server_close()? I can call the method, but it is undocumented (see http://docs.python.org/lib/node634.html). Could someone knowledgable either remove the calls, or add a comment why they're necessary? Thanks. -- Anonymous Coward, 23 Oct 2007

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

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