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:
- class
SimpleHTTPServer.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 class
BaseHTTPServer.BaseHTTPRequestHandler. This class implements thedo_GET()anddo_HEAD()functions.The following are defined as class-level attributes of
SimpleHTTPRequestHandler: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 be
application/octet-stream. The mapping is used case-insensitively,and so should contain only lower-cased keys.
The
SimpleHTTPRequestHandlerclass defines the following methods:do_HEAD()¶This method serves the
'HEAD'request type: it sends the headers itwould send for the equivalentGETrequest. 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 named
index.htmlorindex.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 a404errorresponse if thelistdir()fails.If the request was mapped to a file, it is opened and the contents arereturned. Any
IOErrorexception 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 with
text/the file is opened in text mode; otherwise binary mode is used.The
test()function in theSimpleHTTPServermodule 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
- Module
BaseHTTPServer Base class implementation for Web server and request handler.
