tornado.tcpserver — Basic IOStream -based TCP server¶
To use TCPServer , define a subclass which overrides the handle_stream method.
To make this server serve SSL traffic, send the ssl_options dictionary argument with the arguments required for the ssl.wrap_socket method, including “certfile” and “keyfile”:
TCPServer initialization follows one of three patterns:
server = TCPServer() server.listen(8888) IOLoop.instance().start()
server = TCPServer() server.bind(8888) server.start(0) # Forks multiple sub-processes IOLoop.instance().start()
sockets = bind_sockets(8888) tornado.process.fork_processes(0) server = TCPServer() server.add_sockets(sockets) IOLoop.instance().start()
New in version 3.1: The max_buffer_size argument.
Starts accepting connections on the given port.
This method may be called more than once to listen on multiple ports. listen takes effect immediately; it is not necessary to call TCPServer.start afterwards. It is, however, necessary to start the IOLoop .
Makes this server start accepting connections on the given sockets.
The sockets parameter is a list of socket objects such as those returned by bind_sockets . add_sockets is typically used in combination with that method and tornado.process.fork_processes to provide greater control over the initialization of a multi-process server.
Singular version of add_sockets . Takes a single socket object.
Binds this server to the given port on the given address.
To start the server, call start . If you want to run this server in a single process, you can call listen as a shortcut to the sequence of bind and start calls.
Address may be either an IP address or hostname. If it’s a hostname, the server will listen on all IP addresses associated with the name. Address may be an empty string or None to listen on all available interfaces. Family may be set to either socket.AF_INET or socket.AF_INET6 to restrict to IPv4 or IPv6 addresses, otherwise both will be used if available.
The backlog argument has the same meaning as for socket.listen .
This method may be called multiple times prior to start to listen on multiple ports or interfaces.
Starts this server in the IOLoop .
By default, we run the server in this process and do not fork any additional child process.
Since we use processes and not threads, there is no shared memory between any server code.
Note that multiple processes are not compatible with the autoreload module (or the autoreload=True option to tornado.web.Application which defaults to True when debug=True ). When using multiple processes, no IOLoops can be created or referenced until after the call to TCPServer.start(n) .
Stops listening for new connections.
Requests currently in progress may still continue after the server is stopped.
Override to handle a new IOStream from an incoming connection.
tornado.tcpserver — Basic IOStream -based TCP server¶
To use TCPServer , define a subclass which overrides the handle_stream method.
To make this server serve SSL traffic, send the ssl_options keyword argument with an ssl.SSLContext object. For compatibility with older versions of Python ssl_options may also be a dictionary of keyword arguments for the 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")) TCPServer(ssl_options=ssl_ctx)
TCPServer initialization follows one of three patterns:
server = TCPServer() server.listen(8888) IOLoop.current().start()
server = TCPServer() server.bind(8888) server.start(0) # Forks multiple sub-processes IOLoop.current().start()
sockets = bind_sockets(8888) tornado.process.fork_processes(0) server = TCPServer() server.add_sockets(sockets) IOLoop.current().start()
New in version 3.1: The max_buffer_size argument.
Starts accepting connections on the given port.
This method may be called more than once to listen on multiple ports. listen takes effect immediately; it is not necessary to call TCPServer.start afterwards. It is, however, necessary to start the IOLoop .
Makes this server start accepting connections on the given sockets.
The sockets parameter is a list of socket objects such as those returned by bind_sockets . add_sockets is typically used in combination with that method and tornado.process.fork_processes to provide greater control over the initialization of a multi-process server.
Singular version of add_sockets . Takes a single socket object.
Binds this server to the given port on the given address.
To start the server, call start . If you want to run this server in a single process, you can call listen as a shortcut to the sequence of bind and start calls.
Address may be either an IP address or hostname. If it’s a hostname, the server will listen on all IP addresses associated with the name. Address may be an empty string or None to listen on all available interfaces. Family may be set to either socket.AF_INET or socket.AF_INET6 to restrict to IPv4 or IPv6 addresses, otherwise both will be used if available.
The backlog argument has the same meaning as for socket.listen .
This method may be called multiple times prior to start to listen on multiple ports or interfaces.
Starts this server in the IOLoop .
By default, we run the server in this process and do not fork any additional child process.
Since we use processes and not threads, there is no shared memory between any server code.
Note that multiple processes are not compatible with the autoreload module (or the autoreload=True option to tornado.web.Application which defaults to True when debug=True ). When using multiple processes, no IOLoops can be created or referenced until after the call to TCPServer.start(n) .
Stops listening for new connections.
Requests currently in progress may still continue after the server is stopped.
Override to handle a new IOStream from an incoming connection.
This method may be a coroutine; if so any exceptions it raises asynchronously will be logged. Accepting of incoming connections will not be blocked by this coroutine.
If this TCPServer is configured for SSL, handle_stream may be called before the SSL handshake has completed. Use SSLIOStream.wait_for_handshake if you need to verify the client’s certificate or use NPN/ALPN.
Changed in version 4.2: Added the option for this method to be a coroutine.
© Copyright 2009-2017, The Tornado Authors. Revision 546ccc14 .
Versions latest Downloads On Read the Docs Project Home Builds Free document hosting provided by Read the Docs.
tornado.tcpserver — Basic IOStream -based TCP server¶
To use TCPServer , define a subclass which overrides the handle_stream method. For example, a simple echo server could be defined like this:
from tornado.tcpserver import TCPServer from tornado.iostream import StreamClosedError from tornado import gen class EchoServer(TCPServer): @gen.coroutine def handle_stream(self, stream, address): while True: try: data = yield stream.read_until(b"\n") yield stream.write(data) except StreamClosedError: break
To make this server serve SSL traffic, send the ssl_options keyword argument with an ssl.SSLContext object. For compatibility with older versions of Python ssl_options may also be a dictionary of keyword arguments for the 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")) TCPServer(ssl_options=ssl_ctx)
TCPServer initialization follows one of three patterns:
server = TCPServer() server.listen(8888) IOLoop.current().start()
server = TCPServer() server.bind(8888) server.start(0) # Forks multiple sub-processes IOLoop.current().start()
sockets = bind_sockets(8888) tornado.process.fork_processes(0) server = TCPServer() server.add_sockets(sockets) IOLoop.current().start()
New in version 3.1: The max_buffer_size argument.
Starts accepting connections on the given port.
This method may be called more than once to listen on multiple ports. listen takes effect immediately; it is not necessary to call TCPServer.start afterwards. It is, however, necessary to start the IOLoop .
Makes this server start accepting connections on the given sockets.
The sockets parameter is a list of socket objects such as those returned by bind_sockets . add_sockets is typically used in combination with that method and tornado.process.fork_processes to provide greater control over the initialization of a multi-process server.
Singular version of add_sockets . Takes a single socket object.
Binds this server to the given port on the given address.
To start the server, call start . If you want to run this server in a single process, you can call listen as a shortcut to the sequence of bind and start calls.
Address may be either an IP address or hostname. If it’s a hostname, the server will listen on all IP addresses associated with the name. Address may be an empty string or None to listen on all available interfaces. Family may be set to either socket.AF_INET or socket.AF_INET6 to restrict to IPv4 or IPv6 addresses, otherwise both will be used if available.
The backlog argument has the same meaning as for socket.listen . The reuse_port argument has the same meaning as for bind_sockets .
This method may be called multiple times prior to start to listen on multiple ports or interfaces.
Changed in version 4.4: Added the reuse_port argument.
Starts this server in the IOLoop .
By default, we run the server in this process and do not fork any additional child process.
Since we use processes and not threads, there is no shared memory between any server code.
Note that multiple processes are not compatible with the autoreload module (or the autoreload=True option to tornado.web.Application which defaults to True when debug=True ). When using multiple processes, no IOLoops can be created or referenced until after the call to TCPServer.start(n) .
Stops listening for new connections.
Requests currently in progress may still continue after the server is stopped.
Override to handle a new IOStream from an incoming connection.
This method may be a coroutine; if so any exceptions it raises asynchronously will be logged. Accepting of incoming connections will not be blocked by this coroutine.
If this TCPServer is configured for SSL, handle_stream may be called before the SSL handshake has completed. Use SSLIOStream.wait_for_handshake if you need to verify the client’s certificate or use NPN/ALPN.
Changed in version 4.2: Added the option for this method to be a coroutine.
© Copyright 2009-2016, The Tornado Authors. Revision 4ecc31a5 .
Versions latest Downloads On Read the Docs Project Home Builds Free document hosting provided by Read the Docs.