20.19.SimpleHTTPServer — Simple HTTP request handler

Note

TheSimpleHTTPServer module has been merged intohttp.server inPython 3. The2to3 tool will automatically adapt imports whenconverting your sources to Python 3.

Warning

SimpleHTTPServer is not recommended for production. It only implementsbasic security checks.

TheSimpleHTTPServer module defines a single class,SimpleHTTPRequestHandler, which is interface-compatible withBaseHTTPServer.BaseHTTPRequestHandler.

TheSimpleHTTPServer module defines the following class:

classSimpleHTTPServer.SimpleHTTPRequestHandler(request,client_address,server)

This class serves files from the current directory and below, directlymapping the directory structure to HTTP requests.

A lot of the work, such as parsing the request, is done by the base classBaseHTTPServer.BaseHTTPRequestHandler. This class implements thedo_GET() anddo_HEAD() functions.

The following are defined as class-level attributes ofSimpleHTTPRequestHandler:

server_version

This will be"SimpleHTTP/"+__version__, where__version__ isdefined at the module level.

extensions_map

A dictionary mapping suffixes into MIME types. The default issignified by an empty string, and is considered to beapplication/octet-stream. The mapping is used case-insensitively,and so should contain only lower-cased keys.

TheSimpleHTTPRequestHandler class defines the following methods:

do_HEAD()

This method serves the'HEAD' request type: it sends the headers itwould send for the equivalentGET request. See thedo_GET()method for a more complete explanation of the possible headers.

do_GET()

The request is mapped to a local file by interpreting the request as apath relative to the current working directory.

If the request was mapped to a directory, the directory is checked for afile namedindex.html orindex.htm (in that order). If found, thefile’s contents are returned; otherwise a directory listing is generatedby calling thelist_directory() method. This method usesos.listdir() to scan the directory, and returns a404 errorresponse if thelistdir() fails.

If the request was mapped to a file, it is opened and the contents arereturned. AnyIOError exception in opening the requested file ismapped to a404,'Filenotfound' error. Otherwise, the contenttype is guessed by calling theguess_type() method, which in turnuses theextensions_map variable.

A'Content-type:' header with the guessed content type is output,followed by a'Content-Length:' header with the file’s size and a'Last-Modified:' header with the file’s modification time.

Then follows a blank line signifying the end of the headers, and then thecontents of the file are output. If the file’s MIME type starts withtext/ the file is opened in text mode; otherwise binary mode is used.

Thetest() function in theSimpleHTTPServer module is anexample which creates a server using theSimpleHTTPRequestHandleras the Handler.

New in version 2.5:The'Last-Modified' header.

TheSimpleHTTPServer module can be used in the following manner in orderto set up a very basic web server serving files relative to the currentdirectory.

importSimpleHTTPServerimportSocketServerPORT=8000Handler=SimpleHTTPServer.SimpleHTTPRequestHandlerhttpd=SocketServer.TCPServer(("",PORT),Handler)print"serving at port",PORThttpd.serve_forever()

TheSimpleHTTPServer module can also be invoked directly using the-m switch of the interpreter with aportnumber argument.Similar to the previous example, this serves the files relative to thecurrent directory.

python-mSimpleHTTPServer8000

See also

ModuleBaseHTTPServer

Base class implementation for Web server and request handler.