Back to index

python3.2  3.2.2
Classes | Functions | Variables
http.server Namespace Reference

Classes

class  HTTPServer
class  BaseHTTPRequestHandler
class  SimpleHTTPRequestHandler
class  CGIHTTPRequestHandler

Functions

def _quote_html
def _url_collapse_path_split
def nobody_uid
def executable
def test

Variables

string __version__ = "0.6"
list __all__ = ["HTTPServer", "BaseHTTPRequestHandler"]
string DEFAULT_ERROR_MESSAGE
string DEFAULT_ERROR_CONTENT_TYPE = "text/html;charset=utf-8"
 nobody = None

Detailed Description

HTTP server classes.

Note: BaseHTTPRequestHandler doesn't implement any HTTP request; see
SimpleHTTPRequestHandler for simple implementations of GET, HEAD and POST,
and CGIHTTPRequestHandler for CGI scripts.

It does, however, optionally implement HTTP/1.1 persistent connections,
as of version 0.3.

Notes on CGIHTTPRequestHandler
------------------------------

This class implements GET and POST requests to cgi-bin scripts.

If the os.fork() function is not present (e.g. on Windows),
subprocess.Popen() is used as a fallback, with slightly altered semantics.

In all cases, the implementation is intentionally naive -- all
requests are executed synchronously.

SECURITY WARNING: DON'T USE THIS CODE UNLESS YOU ARE INSIDE A FIREWALL
-- it may execute arbitrary Python code or external programs.

Note that status code 200 is sent prior to execution of a CGI script, so
scripts cannot send other status codes such as 302 (redirect).

XXX To do:

- log requests even later (to capture byte count)
- log user-agent header and other interesting goodies
- send error log to separate file

Function Documentation

def http.server._quote_html (   html) [private]

Definition at line 123 of file server.py.

00123 
00124 def _quote_html(html):
00125     return html.replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;")

Here is the call graph for this function:

Here is the caller graph for this function:

def http.server._url_collapse_path_split (   path) [private]
Given a URL path, remove extra '/'s and '.' path elements and collapse
any '..' references.

Implements something akin to RFC-2396 5.2 step 6 to parse relative paths.

Returns: A tuple of (head, tail) where tail is everything after the final /
and head is everything before it.  Head will always start with a '/' and,
if it contains anything else, never have a trailing '/'.

Raises: IndexError if too many '..' occur within the path.

Definition at line 832 of file server.py.

00832 
00833 def _url_collapse_path_split(path):
00834     """
00835     Given a URL path, remove extra '/'s and '.' path elements and collapse
00836     any '..' references.
00837 
00838     Implements something akin to RFC-2396 5.2 step 6 to parse relative paths.
00839 
00840     Returns: A tuple of (head, tail) where tail is everything after the final /
00841     and head is everything before it.  Head will always start with a '/' and,
00842     if it contains anything else, never have a trailing '/'.
00843 
00844     Raises: IndexError if too many '..' occur within the path.
00845     """
00846     # Similar to os.path.split(os.path.normpath(path)) but specific to URL
00847     # path semantics rather than local operating system semantics.
00848     path_parts = []
00849     for part in path.split('/'):
00850         if part == '.':
00851             path_parts.append('')
00852         else:
00853             path_parts.append(part)
00854     # Filter out blank non trailing parts before consuming the '..'.
00855     path_parts = [part for part in path_parts[:-1] if part] + path_parts[-1:]
00856     if path_parts:
00857         tail_part = path_parts.pop()
00858     else:
00859         tail_part = ''
00860     head_parts = []
00861     for part in path_parts:
00862         if part == '..':
00863             head_parts.pop()
00864         else:
00865             head_parts.append(part)
00866     if tail_part and tail_part == '..':
00867         head_parts.pop()
00868         tail_part = ''
00869     return ('/' + '/'.join(head_parts), tail_part)
00870 

Here is the caller graph for this function:

def http.server.executable (   path)
Test for executable file.

Definition at line 889 of file server.py.

00889 
00890 def executable(path):
00891     """Test for executable file."""
00892     try:
00893         st = os.stat(path)
00894     except os.error:
00895         return False
00896     return st.st_mode & 0o111 != 0
00897 

Here is the caller graph for this function:

Internal routine to get nobody's uid

Definition at line 873 of file server.py.

00873 
00874 def nobody_uid():
00875     """Internal routine to get nobody's uid"""
00876     global nobody
00877     if nobody:
00878         return nobody
00879     try:
00880         import pwd
00881     except ImportError:
00882         return -1
00883     try:
00884         nobody = pwd.getpwnam('nobody')[2]
00885     except KeyError:
00886         nobody = 1 + max(x[2] for x in pwd.getpwall())
00887     return nobody
00888 

Here is the call graph for this function:

Here is the caller graph for this function:

def http.server.test (   HandlerClass = BaseHTTPRequestHandler,
  ServerClass = HTTPServer,
  protocol = "HTTP/1.0" 
)
Test the HTTP request handler class.

This runs an HTTP server on port 8000 (or the first command line
argument).

Definition at line 1163 of file server.py.

01163 
01164          ServerClass = HTTPServer, protocol="HTTP/1.0"):
01165     """Test the HTTP request handler class.
01166 
01167     This runs an HTTP server on port 8000 (or the first command line
01168     argument).
01169 
01170     """
01171 
01172     if sys.argv[1:]:
01173         port = int(sys.argv[1])
01174     else:
01175         port = 8000
01176     server_address = ('', port)
01177 
01178     HandlerClass.protocol_version = protocol
01179     httpd = ServerClass(server_address, HandlerClass)
01180 
01181     sa = httpd.socket.getsockname()
01182     print("Serving HTTP on", sa[0], "port", sa[1], "...")
01183     try:
01184         httpd.serve_forever()
01185     except KeyboardInterrupt:
01186         print("\nKeyboard interrupt received, exiting.")
01187         httpd.server_close()
01188         sys.exit(0)

Here is the caller graph for this function:


Variable Documentation

list http.server.__all__ = ["HTTPServer", "BaseHTTPRequestHandler"]

Definition at line 85 of file server.py.

Definition at line 83 of file server.py.

string http.server.DEFAULT_ERROR_CONTENT_TYPE = "text/html;charset=utf-8"

Definition at line 121 of file server.py.

Initial value:
00001 """\
00002 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
00003         "http://www.w3.org/TR/html4/strict.dtd">
00004     <head>
00005         <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
00006         <title>Error response</title>
00007     </head>
00008     <body>
00009         <h1>Error response</h1>
00010         <p>Error code: %(code)d</p>
00011         <p>Message: %(message)s.</p>
00012         <p>Error code explanation: %(code)s - %(explain)s.</p>
00013     </body>
00014 </html>
00015 """

Definition at line 105 of file server.py.

Definition at line 871 of file server.py.