cyclone.httpserver — Non-blocking HTTP server

A non-blocking, single-threaded HTTP server.

Typical applications have little direct interaction with the HTTPConnection class, which is the HTTP parser executed on incoming connections.

It is a protocol class that inherits Twisted’s LineReceiver, and is usually created by cyclone.web.Application, our connection factory.

This module also defines the HTTPRequest class which is exposed via cyclone.web.RequestHandler.request.

HTTPRequest objects

class cyclone.httpserver.HTTPRequest(method, uri, version='HTTP/1.0', headers=None, body=None, remote_ip=None, protocol=None, host=None, files=None, connection=None)[source]

A single HTTP request.

All attributes are type str unless otherwise noted.

method

HTTP request method, e.g. “GET” or “POST”

uri

The requested uri.

path

The path portion of uri

query

The query portion of uri

version

HTTP version specified in request, e.g. “HTTP/1.1”

headers

HTTPHeader dictionary-like object for request headers. Acts like a case-insensitive dictionary with additional methods for repeated headers.

body

Request body, if present, as a byte string.

remote_ip

Client’s IP address as a string. If HTTPConnection.xheaders is set, will pass along the real IP address provided by a load balancer in the X-Real-Ip header

protocol

The protocol used, either “http” or “https”. If HTTPConnection.xheaders is set, will pass along the protocol used by a load balancer if reported via an X-Scheme header.

host

The requested hostname, usually taken from the Host header.

arguments

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.

files

File uploads are available in the files property, which maps file names to lists of HTTPFile.

connection

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.

supports_http_1_1()[source]

Returns True if this request supports HTTP/1.1 semantics

cookies[source]

A dictionary of Cookie.Morsel objects.

write(chunk)[source]

Writes the given chunk to the response stream.

finish()[source]

Finishes this HTTP request on the open connection.

full_url()[source]

Reconstructs the full URL for this request.

request_time()[source]

Returns the amount of time it took for this request to execute.

notifyFinish()[source]

Returns a Deferred object, which is fired when the request is finished and the connection is closed.

HTTP Server

class cyclone.httpserver.HTTPConnection[source]

Handles a connection to an HTTP client, executing HTTP requests.

We parse HTTP headers and bodies, and execute the request callback until the HTTP conection is closed.

If xheaders is True, we support the X-Real-Ip and X-Scheme headers, which override the remote IP and HTTP scheme for all requests. These headers are useful when running Tornado behind a reverse proxy or load balancer.