tornado.httpserver — Non-blocking HTTP server¶

A non-blocking, single-threaded HTTP server.

Typical applications have little direct interaction with the HTTPServerclass except to start a server at the beginning of the process(and even that is often done indirectly via tornado.web.Application.listen).

在 4.0 版更改: The HTTPRequest class that used to live in this module has been movedto tornado.httputil.HTTPServerRequest. The old name remains as an alias.

HTTP Server¶

class tornado.httpserver.HTTPServer(*args, **kwargs)[源代码]

A non-blocking, single-threaded HTTP server.

A server is defined by a subclass of HTTPServerConnectionDelegate,or, for backwards compatibility, a callback that takes anHTTPServerRequest as an argument. The delegate is usually atornado.web.Application.

HTTPServer supports keep-alive connections by default(automatically for HTTP/1.1, or for HTTP/1.0 when the clientrequests Connection: keep-alive).

If xheaders is True, we support theX-Real-Ip/X-Forwarded-For andX-Scheme/X-Forwarded-Proto headers, which override theremote IP and URI scheme/protocol for all requests. These headersare useful when running Tornado behind a reverse proxy or loadbalancer. The protocol argument can also be set to httpsif Tornado is run behind an SSL-decoding proxy that does not set one ofthe supported xheaders.

To make this server serve SSL traffic, send the ssloptions keywordargument with an ssl.SSLContext object. For compatibility with olderversions of Python ssl_options may also be a dictionary of keywordarguments for the ssl.wrap_socket method.:



  1. 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)




HTTPServer initialization follows one of three patterns (theinitialization methods are defined on tornado.tcpserver.TCPServer):

    -
    listen: simple single-process:



    server = HTTPServer(app)
    server.listen(8888)
    IOLoop.current().start()




    In many cases, tornado.web.Application.listen can be used to avoidthe need to explicitly create the HTTPServer.

    -
    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 IOLoop must _not
    be passedto the HTTPServer constructor. start will always startthe server on the default singleton IOLoop.

    -
    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_sockets interface is more complicated,but it can be used with tornado.process.fork_processes togive you more flexibility in when the fork happens.add_sockets can also be used in single-processservers if you want to create your listening sockets in someway other than tornado.netutil.bind_sockets.


在 4.0 版更改: Added decompress_request, chunk_size, max_header_size,idle_connection_timeout, body_timeout, max_body_sizearguments. Added support for HTTPServerConnectionDelegateinstances as request_callback.



在 4.1 版更改: HTTPServerConnectionDelegate.start_request is now called withtwo arguments (server_conn, request_conn) (in accordance with thedocumentation) instead of one (request_conn).



在 4.2 版更改: HTTPServer is now a subclass of tornado.util.Configurable.

原文:

https://tornado-zh-cn.readthedocs.io/zh_CN/latest/httpserver.html