### Navigation
- [index](# "General Index")
- [modules](# "Python Module Index") |
- [next](# "tornado.httpclient — Asynchronous HTTP client") |
- [previous](# "HTTP servers and clients") |
- [Tornado 4.4.dev1 documentation](#) »
- [HTTP servers and clients](#) »
# `tornado.httpserver` — Non-blocking HTTP server
A non-blocking, single-threaded HTTP server.
Typical applications have little direct interaction with the [`HTTPServer`](# "tornado.httpserver.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`](# "tornado.web.Application.listen")).
Changed in version 4.0: The `HTTPRequest` class that used to live in this module has been movedto [`tornado.httputil.HTTPServerRequest`](# "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`](# "tornado.httputil.HTTPServerConnectionDelegate"),or, for backwards compatibility, a callback that takes an[`HTTPServerRequest`](# "tornado.httputil.HTTPServerRequest") as an argument. The delegate is usually a[`tornado.web.Application`](# "tornado.web.Application").
[`HTTPServer`](# "tornado.httpserver.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 the`X-Real-Ip`/`X-Forwarded-For` and`X-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 `https`if 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 `ssl_options` keywordargument with an [`ssl.SSLContext`](https://docs.python.org/3.4/library/ssl.html#ssl.SSLContext "(in Python v3.4)") [https://docs.python.org/3.4/library/ssl.html#ssl.SSLContext] object. For compatibility with olderversions of Python `ssl_options` may also be a dictionary of keywordarguments for the [`ssl.wrap_socket`](https://docs.python.org/3.4/library/ssl.html#ssl.wrap_socket "(in Python v3.4)") [https://docs.python.org/3.4/library/ssl.html#ssl.wrap_socket] method.:
~~~
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`](# "tornado.httpserver.HTTPServer") initialization follows one of three patterns (theinitialization methods are defined on [`tornado.tcpserver.TCPServer`](# "tornado.tcpserver.TCPServer")):
1.
[`listen`](# "tornado.tcpserver.TCPServer.listen"): simple single-process:
~~~
server = HTTPServer(app)
server.listen(8888)
IOLoop.current().start()
~~~
In many cases, [`tornado.web.Application.listen`](# "tornado.web.Application.listen") can be used to avoidthe need to explicitly create the [`HTTPServer`](# "tornado.httpserver.HTTPServer").
1.
[`bind`](# "tornado.tcpserver.TCPServer.bind")/[`start`](# "tornado.tcpserver.TCPServer.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`](# "tornado.ioloop.IOLoop") must *not* be passedto the [`HTTPServer`](# "tornado.httpserver.HTTPServer") constructor. [`start`](# "tornado.tcpserver.TCPServer.start") will always startthe server on the default singleton [`IOLoop`](# "tornado.ioloop.IOLoop").
1.
[`add_sockets`](# "tornado.tcpserver.TCPServer.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`](# "tornado.tcpserver.TCPServer.add_sockets") interface is more complicated,but it can be used with [`tornado.process.fork_processes`](# "tornado.process.fork_processes") togive you more flexibility in when the fork happens.[`add_sockets`](# "tornado.tcpserver.TCPServer.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`](# "tornado.netutil.bind_sockets").
Changed in version 4.0: Added `decompress_request`, `chunk_size`, `max_header_size`,`idle_connection_timeout`, `body_timeout`, `max_body_size`arguments. Added support for [`HTTPServerConnectionDelegate`](# "tornado.httputil.HTTPServerConnectionDelegate")instances as `request_callback`.
Changed in version 4.1: [`HTTPServerConnectionDelegate.start_request`](# "tornado.httputil.HTTPServerConnectionDelegate.start_request") is now called withtwo arguments `(server_conn, request_conn)` (in accordance with thedocumentation) instead of one `(request_conn)`.
Changed in version 4.2: [`HTTPServer`](# "tornado.httpserver.HTTPServer") is now a subclass of [`tornado.util.Configurable`](# "tornado.util.Configurable").
© Copyright 2009-2016, The Tornado Authors. Created using [Sphinx](http://sphinx-doc.org/) 1.3.5.
- User's guide
- Introduction
- Asynchronous and non-Blocking I/O
- Coroutines
- Queue example - a concurrent web spider
- Structure of a Tornado web application
- Templates and UI
- Authentication and security
- Running and deploying
- Web framework
- tornado.web — RequestHandler and Application classes
- tornado.template — Flexible output generation
- tornado.escape — Escaping and string manipulation
- tornado.locale — Internationalization support
- tornado.websocket — Bidirectional communication to the browser
- HTTP servers and clients
- tornado.httpserver — Non-blocking HTTP server
- tornado.httpclient — Asynchronous HTTP client
- tornado.httputil — Manipulate HTTP headers and URLs
- tornado.http1connection – HTTP/1.x client/server implementation
- Asynchronous networking
- tornado.ioloop — Main event loop
- tornado.iostream — Convenient wrappers for non-blocking sockets
- tornado.netutil — Miscellaneous network utilities
- tornado.tcpclient — IOStream connection factory
- tornado.tcpserver — Basic IOStream-based TCP server
- Coroutines and concurrency
- tornado.gen — Simplify asynchronous code
- tornado.concurrent — Work with threads and futures
- tornado.locks – Synchronization primitives
- tornado.queues – Queues for coroutines
- tornado.process — Utilities for multiple processes
- Integration with other services
- tornado.auth — Third-party login with OpenID and OAuth
- tornado.wsgi — Interoperability with other Python frameworks and servers
- tornado.platform.asyncio — Bridge between asyncio and Tornado
- tornado.platform.caresresolver — Asynchronous DNS Resolver using C-Ares
- tornado.platform.twisted — Bridges between Twisted and Tornado
- Utilities
- tornado.autoreload — Automatically detect code changes in development
- tornado.log — Logging support
- tornado.options — Command-line parsing
- tornado.stack_context — Exception handling across asynchronous callbacks
- tornado.testing — Unit testing support for asynchronous code
- tornado.util — General-purpose utilities
- Frequently Asked Questions
- Release notes
- What's new in Tornado 4.3
- What's new in Tornado 4.2.1
- What's new in Tornado 4.2
- What's new in Tornado 4.1
- What's new in Tornado 4.0.2
- What's new in Tornado 4.0.1
- What's new in Tornado 4.0
- What's new in Tornado 3.2.2
- What's new in Tornado 3.2.1
- What's new in Tornado 3.2
- What's new in Tornado 3.1.1
- What's new in Tornado 3.1
- What's new in Tornado 3.0.2
- What's new in Tornado 3.0.1
- What's new in Tornado 3.0
- What's new in Tornado 2.4.1
- What's new in Tornado 2.4
- What's new in Tornado 2.3
- What's new in Tornado 2.2.1
- What's new in Tornado 2.2
- What's new in Tornado 2.1.1
- What's new in Tornado 2.1
- What's new in Tornado 2.0
- What's new in Tornado 1.2.1
- What's new in Tornado 1.2
- What's new in Tornado 1.1.1
- What's new in Tornado 1.1
- What's new in Tornado 1.0.1
- What's new in Tornado 1.0