tornado.tcpserver — Basic IOStream-based TCP server¶
A non-blocking, single-threaded TCP server.
- class
tornado.tcpserver.
TCPServer
(io_loop=None, ssl_options=None, max_buffer_size=None, read_chunk_size=None)[源代码]¶
A non-blocking, single-threaded TCP server.
To useTCPServer
, define a subclass which overrides thehandle_stream
method.
To make this server serve SSL traffic, send thessloptions
keywordargument with anssl.SSLContext
object. For compatibility with olderversions of Pythonssl_options
may also be a dictionary of keywordarguments for thessl.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:
-
listen: simple single-process:
server = TCPServer()
server.listen(8888)
IOLoop.current().start()
-
bind/start: simple multi-process:
server = TCPServer()
server.bind(8888)
server.start(0) # Forks multiple sub-processes
IOLoop.current().start()
When using this interface, an IOLoop must _not be passedto the TCPServer constructor. start will always startthe server on the default singleton IOLoop.
-
add_sockets: advanced multi-process:
sockets = bindsockets(8888)
tornado.process.fork_processes(0)
server = TCPServer()
server.add_sockets(sockets)
IOLoop.current().start()
The add_sockets interface is more complicated, but it can beused with tornado.process.fork_processes to give you moreflexibility in when the fork happens. add_sockets canalso be used in single-process servers if you want to createyour listening sockets in some way other thanbind_sockets.
3.1 新版功能: Themax_buffer_size
argument.listen
(_port, address='')[源代码]¶
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 callTCPServer.start
afterwards. It is, however, necessary to starttheIOLoop
.
addsockets
(_sockets)[源代码]¶
Makes this server start accepting connections on the given sockets.
Thesockets
parameter is a list of socket objects such asthose returned bybind_sockets
.add_sockets
is typically used in combination with thatmethod andtornado.process.fork_processes
to provide greatercontrol over the initialization of a multi-process server.
addsocket
(_socket)[源代码]¶
Singular version ofadd_sockets
. Takes a single socket object.
bind
(port, address=None, family=0, backlog=128, reuse_port=False)[源代码]¶
Binds this server to the given port on the given address.
To start the server, callstart
. If you want to run this serverin a single process, you can calllisten
as a shortcut to thesequence ofbind
andstart
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 thename. Address may be an empty string or None to listen on allavailable interfaces. Family may be set to eithersocket.AF_INET
orsocket.AF_INET6
to restrict to IPv4 or IPv6 addresses, otherwiseboth will be used if available.
Thebacklog
argument has the same meaning as forsocket.listen
. Thereuseport
argumenthas the same meaning as forbind_sockets
.
This method may be called multiple times prior tostart
to listenon multiple ports or interfaces.
在 4.4 版更改: Added thereuse_port
argument.
start
(_num_processes=1)[源代码]¶
Starts this server in theIOLoop
.
By default, we run the server in this process and do not fork anyadditional child process.
If numprocesses isNone
or <= 0, we detect the number of coresavailable on this machine and fork that number of childprocesses. If num_processes is given and > 1, we fork thatspecific number of sub-processes.
Since we use processes and not threads, there is no shared memorybetween any server code.
Note that multiple processes are not compatible with the autoreloadmodule (or theautoreload=True
option totornado.web.Application
which defaults to True whendebug=True
).When using multiple processes, no IOLoops can be created orreferenced until after the call toTCPServer.start(n)
.
stop
()[源代码]¶
Stops listening for new connections.
Requests currently in progress may still continue after theserver is stopped.
handle_stream
(_stream, address)[源代码]¶
Override to handle a newIOStream
from an incoming connection.
This method may be a coroutine; if so any exceptions it raisesasynchronously will be logged. Accepting of incoming connectionswill not be blocked by this coroutine.
If thisTCPServer
is configured for SSL,handle_stream
may be called before the SSL handshake has completed. UseSSLIOStream.wait_for_handshake
if you need to verify the client’scertificate or use NPN/ALPN.
在 4.2 版更改: Added the option for this method to be a coroutine.
- ssl_ctx = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)
原文:
https://tornado-zh-cn.readthedocs.io/zh_CN/latest/tcpserver.html