tornado.httpserver — Non-blocking HTTP server¶
A non-blocking, single-threaded HTTP server.
Typical applications have little direct interaction with the HTTPServer
class except to start a server at the beginning of the process
(and even that is often done indirectly via tornado.web.Application.listen).
Changed in version 4.0: The HTTPRequest class that used to live in this module has been moved
to tornado.httputil.HTTPServerRequest. The old name remains as an alias.
HTTP Server¶
-
class
tornado.httpserver.HTTPServer(*args, **kwargs)[source]¶ A non-blocking, single-threaded HTTP server.
A server is defined by a subclass of
HTTPServerConnectionDelegate, or, for backwards compatibility, a callback that takes anHTTPServerRequestas an argument. The delegate is usually atornado.web.Application.HTTPServersupports keep-alive connections by default (automatically for HTTP/1.1, or for HTTP/1.0 when the client requestsConnection: keep-alive).If
xheadersisTrue, we support theX-Real-Ip/X-Forwarded-ForandX-Scheme/X-Forwarded-Protoheaders, which override the remote IP and URI scheme/protocol for all requests. These headers are useful when running Tornado behind a reverse proxy or load balancer. Theprotocolargument can also be set tohttpsif Tornado is run behind an SSL-decoding proxy that does not set one of the supportedxheaders.By default, when parsing the
X-Forwarded-Forheader, Tornado will select the last (i.e., the closest) address on the list of hosts as the remote host IP address. To select the next server in the chain, a list of trusted downstream hosts may be passed as thetrusted_downstreamargument. These hosts will be skipped when parsing theX-Forwarded-Forheader.To make this server serve SSL traffic, send the
ssl_optionskeyword argument with anssl.SSLContextobject. For compatibility with older versions of Pythonssl_optionsmay also be a dictionary of keyword arguments for thessl.wrap_socketmethod.:ssl_ctx = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH) ssl_ctx.load_cert_chain(os.path.join(data_dir, "mydomain.crt"), os.path.join(data_dir, "mydomain.key")) HTTPServer(applicaton, ssl_options=ssl_ctx)
HTTPServerinitialization follows one of three patterns (the initialization methods are defined ontornado.tcpserver.TCPServer):listen: simple single-process:server = HTTPServer(app) server.listen(8888) IOLoop.current().start()
In many cases,
tornado.web.Application.listencan be used to avoid the need to explicitly create theHTTPServer.bind/start: simple multi-process:server = HTTPServer(app) server.bind(8888) server.start(0) # Forks multiple sub-processes IOLoop.current().start()
When using this interface, an
IOLoopmust not be passed to theHTTPServerconstructor.startwill always start the server on the default singletonIOLoop.add_sockets: advanced multi-process:sockets = tornado.netutil.bind_sockets(8888) tornado.process.fork_processes(0) server = HTTPServer(app) server.add_sockets(sockets) IOLoop.current().start()
The
add_socketsinterface is more complicated, but it can be used withtornado.process.fork_processesto give you more flexibility in when the fork happens.add_socketscan also be used in single-process servers if you want to create your listening sockets in some way other thantornado.netutil.bind_sockets.
Changed in version 4.0: Added
decompress_request,chunk_size,max_header_size,idle_connection_timeout,body_timeout,max_body_sizearguments. Added support forHTTPServerConnectionDelegateinstances asrequest_callback.Changed in version 4.1:
HTTPServerConnectionDelegate.start_requestis now called with two arguments(server_conn, request_conn)(in accordance with the documentation) instead of one(request_conn).Changed in version 4.2:
HTTPServeris now a subclass oftornado.util.Configurable.Changed in version 4.5: Added the
trusted_downstreamargument.