TheSimpleHTTPServer module defines a request-handler class,interface-compatible withBaseHTTPServer.BaseHTTPRequestHandler,that serves files only from a base directory.
TheSimpleHTTPServer module defines the following class:
| request, client_address, server) |
A lot of the work, such as parsing the request, is done by the baseclassBaseHTTPServer.BaseHTTPRequestHandler. This classimplements thedo_GET() anddo_HEAD() functions.
TheSimpleHTTPRequestHandler defines the following membervariables:
"SimpleHTTP/" + __version__, where__version__is defined in the module.application/octet-stream.The mapping is used case-insensitively, and so should contain onlylower-cased keys.TheSimpleHTTPRequestHandler defines the following methods:
| ) |
'HEAD' request type: it sends theheaders it would send for the equivalentGET request. See thedo_GET() method for a more complete explanation of the possibleheaders.| ) |
If the request was mapped to a directory, the directory is checked fora file namedindex.html orindex.htm (in that order).If found, the file's contents are returned; otherwise a directorylisting is generated by calling thelist_directory() method.This method usesos.listdir() to scan the directory, andreturns a404 error response if thelistdir() fails.
If the request was mapped to a file, it is opened and the contents arereturned. AnyIOError exception in opening the requestedfile is mapped to a404,'File not found'error. Otherwise, the content type is guessed by calling theguess_type() method, which in turn uses theextensions_map variable.
A'Content-type:' header with the guessed content type isoutput, followed by a'Content-Length:' header with the file'ssize and a'Last-Modified:' header with the file's modificationtime.
Then follows a blank line signifying the end of the headers,and then the contents of the file are output. If the file's MIME typestarts withtext/ the file is opened in text mode; otherwisebinary mode is used.
For example usage, see the implementation of thetest()function.New in version 2.5:The'Last-Modified' header.
See Also: