HTTP utility code shared by clients and servers.
This module also defines the HTTPServerRequest class which is exposed via tornado.web.RequestHandler.request.
A dictionary that maintains Http-Header-Case for all keys.
Supports multiple values per key via a pair of new methods, add() and get_list(). The regular dictionary interface returns a single value per key, with multiple values joined by a comma.
>>> h = HTTPHeaders({"content-type": "text/html"})
>>> list(h.keys())
['Content-Type']
>>> h["Content-Type"]
'text/html'
>>> h.add("Set-Cookie", "A=B")
>>> h.add("Set-Cookie", "C=D")
>>> h["set-cookie"]
'A=B,C=D'
>>> h.get_list("set-cookie")
['A=B', 'C=D']
>>> for (k,v) in sorted(h.get_all()):
... print('%s: %s' % (k,v))
...
Content-Type: text/html
Set-Cookie: A=B
Set-Cookie: C=D
Returns an iterable of all (name, value) pairs.
If a header has multiple values, multiple pairs will be returned with the same name.
A single HTTP request.
All attributes are type str unless otherwise noted.
HTTP request method, e.g. “GET” or “POST”
The requested uri.
HTTP version specified in request, e.g. “HTTP/1.1”
HTTPHeaders dictionary-like object for request headers. Acts like a case-insensitive dictionary with additional methods for repeated headers.
Request body, if present, as a byte string.
Client’s IP address as a string. If HTTPServer.xheaders is set, will pass along the real IP address provided by a load balancer in the X-Real-Ip or X-Forwarded-For header.
Changed in version 3.1: The list format of X-Forwarded-For is now supported.
The protocol used, either “http” or “https”. If HTTPServer.xheaders is set, will pass along the protocol used by a load balancer if reported via an X-Scheme header.
The requested hostname, usually taken from the Host header.
GET/POST arguments are available in the arguments property, which maps arguments names to lists of values (to support multiple values for individual names). Names are of type str, while arguments are byte strings. Note that this is different from RequestHandler.get_argument, which returns argument values as unicode strings.
Same format as arguments, but contains only arguments extracted from the query string.
New in version 3.2.
Same format as arguments, but contains only arguments extracted from the request body.
New in version 3.2.
File uploads are available in the files property, which maps file names to lists of HTTPFile.
An HTTP request is attached to a single HTTP connection, which can be accessed through the “connection” attribute. Since connections are typically kept open in HTTP/1.1, multiple requests can be handled sequentially on a single connection.
Changed in version 3.3: Moved from tornado.httpserver.HTTPRequest.
Returns True if this request supports HTTP/1.1 semantics.
Deprecated since version 3.3: Applications are less likely to need this information with the introduction of HTTPConnection. If you still need it, access the version attribute directly.
A dictionary of Cookie.Morsel objects.
Writes the given chunk to the response stream.
Deprecated since version 3.3: Use request.connection and the HTTPConnection methods to write the response.
Finishes this HTTP request on the open connection.
Deprecated since version 3.3: Use request.connection and the HTTPConnection methods to write the response.
Returns the client’s SSL certificate, if any.
To use client certificates, the HTTPServer must have been constructed with cert_reqs set in ssl_options, e.g.:
server = HTTPServer(app,
ssl_options=dict(
certfile="foo.crt",
keyfile="foo.key",
cert_reqs=ssl.CERT_REQUIRED,
ca_certs="cacert.crt"))
By default, the return value is a dictionary (or None, if no client certificate is present). If binary_form is true, a DER-encoded form of the certificate is returned instead. See SSLSocket.getpeercert() in the standard library for more details. http://docs.python.org/library/ssl.html#sslsocket-objects
Exception class for malformed HTTP requests or responses from remote sources.
New in version 3.3.
Exception class for errors in HTTP output.
New in version 3.3.
Implement this interface to handle requests from HTTPServer.
New in version 3.3.
This method is called by the server when a new request has started.
Parameters: |
|
---|
This method should return a HTTPMessageDelegate.
Implement this interface to handle an HTTP request or response.
New in version 3.3.
Called when the HTTP headers have been received and parsed.
Parameters: |
|
---|
Some HTTPConnection methods can only be called during headers_received.
May return a Future; if it does the body will not be read until it is done.
Applications use this interface to write their responses.
New in version 3.3.
Write an HTTP header block.
Parameters: |
|
---|
Returns a Future if no callback is given.
Concatenate url and argument dictionary regardless of whether url has existing query parameters.
>>> url_concat("http://example.com/foo?a=b", dict(c="d"))
'http://example.com/foo?a=b&c=d'
Represents a file uploaded via a form.
For backwards compatibility, its instance attributes are also accessible as dictionary keys.
Parses a form request body.
Supports application/x-www-form-urlencoded and multipart/form-data. The content_type parameter should be a string and body should be a byte string. The arguments and files parameters are dictionaries that will be updated with the parsed contents.
Parses a multipart/form-data body.
The boundary and data parameters are both byte strings. The dictionaries given in the arguments and files parameters will be updated with the contents of the body.
Formats a timestamp in the format used by HTTP.
The argument may be a numeric timestamp as returned by time.time, a time tuple as returned by time.gmtime, or a datetime.datetime object.
>>> format_timestamp(1359312200)
'Sun, 27 Jan 2013 18:43:20 GMT'
RequestStartLine(method, path, version)
Alias for field number 0
Alias for field number 1
Alias for field number 2
Returns a (method, path, version) tuple for an HTTP 1.x request line.
The response is a collections.namedtuple.
>>> parse_request_start_line("GET /foo HTTP/1.1")
RequestStartLine(method='GET', path='/foo', version='HTTP/1.1')
ResponseStartLine(version, code, reason)
Alias for field number 1
Alias for field number 2
Alias for field number 0
Returns a (version, code, reason) tuple for an HTTP 1.x response line.
The response is a collections.namedtuple.
>>> parse_response_start_line("HTTP/1.1 200 OK")
ResponseStartLine(version='HTTP/1.1', code=200, reason='OK')