Movatterモバイル変換


[0]ホーム

URL:


Up one LevelPython Library ReferenceContentsModule IndexIndex

18.20SimpleHTTPServer -- Simple HTTP request handler

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:

class SimpleHTTPRequestHandler(request, client_address, server)
This class is used to serve files from the current directory and below,directly mapping the directory structure to HTTP requests.

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:

server_version
This will be"SimpleHTTP/" + __version__, where__version__is defined in the module.

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

TheSimpleHTTPRequestHandler defines the following methods:

do_HEAD()
This method serves the'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.

do_GET()
The request is mapped to a local file by interpreting the request asa path relative to the current working directory.

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:

ModuleBaseHTTPServer:
Base class implementation for Web server and request handler.


Up one LevelPython Library ReferenceContentsModule IndexIndex

Release 2.5.2, documentation updated on 21st February, 2008.
SeeAbout this document... for information on suggesting changes.
[8]ページ先頭

©2009-2025 Movatter.jp