A complete set of headers is sent, followed by text composed using the error_message_format class variable.
BASEHTTPSERVER PYTHON 3 INSTALL CODE
The numeric code specifies the HTTP error code, with message as optional, more specific text. Sends and logs a complete error reply to the client. This method will parse and dispatch the request to the appropriate do_*() method. You should never need to override it instead, implement appropriate do_*() methods. The shortmessage is usually used as the message key in an error response, and longmessage as the explain key (see the error_message_format class variable).Ī BaseHTTPRequestHandler instance has the following methods: handle()Ĭalls handle_one_request() once (or, if persistent connections are enabled, multiple times) to handle incoming HTTP requests. This variable contains a mapping of error code integers to two-element tuples containing a short and long message. Typically, this is not overridden, and it defaults to mimetools.Message. Specifies a rfc822.Message-like class to parse HTTP headers. For backwards compatibility, the setting defaults to 'HTTP/1.0'. If set to 'HTTP/1.1', the server will permit HTTP persistent connections however, your server must then include an accurate Content-Length header (using send_header()) in all of its responses to clients. This specifies the HTTP protocol version used in responses. New in version 2.6: Previously, the content type was always 'text/html'. Specifies the Content-Type HTTP header of error responses sent to the client. Default message and explain values can found in the responses class variable. message should be a string containing a (detailed) error message of what occurred, and explain should be an explanation of the error code number. The code key should be an integer, specifying the numeric HTTP error code value. It uses parenthesized, keyed format specifiers, so the format operand must be a dictionary. Specifies a format string for building an error response to the client. sys_versionĬontains the Python system version, in a form usable by the version_string method and the server_version class variable. The format is multiple whitespace-separated strings, where each string is of the form name. Proper adherence to the HTTP protocol must be used when writing to this stream.īaseHTTPRequestHandler has the following class variables: server_version
wfileĬontains the output stream for writing a response back to the client. rfileĬontains an input stream, positioned at the start of the optional input data. This instance parses and manages the headers in the HTTP request. Holds an instance of the class specified by the MessageClass class variable. request_versionĬontains the version string from the request. serverĬontains the command (request type). Subclasses should not need to override or extend the _init_() method.īaseHTTPRequestHandler has the following instance variables: client_addressĬontains a tuple of the form (host, port) referring to the client’s address. All of the relevant information is stored in instance variables of the handler. For example, for the request method SPAM, the do_SPAM() method will be called with no arguments. The method name is constructed from the request. The handler will parse the request and the headers, then call a method specific to the request type. BaseHTTPRequestHandler provides a number of class and instance variables, and methods for use by subclasses. By itself, it cannot respond to any actual HTTP requests it must be subclassed to handle each request method (e.g. This class is used to handle the HTTP requests that arrive at the server. class BaseHTTPServer.BaseHTTPRequestHandler(request, client_address, server) The server is accessible by the handler, typically through the handler’s server instance variable. This class builds on the TCPServer class by storing the server address as instance variables named server_name and server_port. Httpd = server_class(server_address, handler_class)Ĭlass BaseHTTPServer.HTTPServer(server_address, RequestHandlerClass) Handler_class=BaseHTTPServer.BaseHTTPRequestHandler):
Code to create and run the server looks like this: def run(server_class=BaseHTTPServer.HTTPServer, It creates and listens at the HTTP socket, dispatching the requests to a handler. The first class, HTTPServer, is a SocketServer.TCPServer subclass, and therefore implements the SocketServer.BaseServer interface. See the SimpleHTTPServer and CGIHTTPServer modules. Usually, this module isn’t used directly, but is used as a basis for building functioning Web servers.
This module defines two classes for implementing HTTP servers (Web servers).
The 2to3 tool will automatically adapt imports when converting your sources to Python 3. The BaseHTTPServer module has been merged into rver in Python 3.