当前位置: 首页 > 文档资料 > Tornado 用户手册 >

tornado.tcpserver — Basic IOStream-based TCP server

优质
小牛编辑
133浏览
2023-12-01

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 use , define a subclass which overrides the method.

To make this server serve SSL traffic, send the ssl_options keyword argument with an object. For compatibility with older versions of Python ssl_options may also be a dictionary of keyword arguments for the 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)

initialization follows one of three patterns:

  1. : simple single-process:

    server = TCPServer()
    server.listen(8888)
    IOLoop.current().start()
  2. /: simple multi-process:

    server = TCPServer()
    server.bind(8888)
    server.start(0)  # Forks multiple sub-processes
    IOLoop.current().start()

    When using this interface, an must not be passed to the constructor. will always start the server on the default singleton .

  3. : advanced multi-process:

    sockets = bind_sockets(8888)
    tornado.process.fork_processes(0)
    server = TCPServer()
    server.add_sockets(sockets)
    IOLoop.current().start()

    The interface is more complicated, but it can be used with to give you more flexibility in when the fork happens. can also be used in single-process servers if you want to create your listening sockets in some way other than .

3.1 新版功能: The max_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. takes effect immediately; it is not necessary to call afterwards. It is, however, necessary to start the .

add_sockets(sockets)

Makes this server start accepting connections on the given sockets.

The sockets parameter is a list of socket objects such as those returned by . is typically used in combination with that method and to provide greater control over the initialization of a multi-process server.

add_socket(socket)

Singular version of . 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, call . If you want to run this server in a single process, you can call as a shortcut to the sequence of and 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 or to restrict to IPv4 or IPv6 addresses, otherwise both will be used if available.

The backlog argument has the same meaning as for . The reuse_port argument has the same meaning as for .

This method may be called multiple times prior to to listen on multiple ports or interfaces.

在 4.4 版更改: Added the reuse_port argument.

start(num_processes=1)

Starts this server in the .

By default, we run the server in this process and do not fork any additional child process.

If num_processes is None or <= 0, we detect the number of cores available on this machine and fork that number of child processes. If num_processes is given and > 1, we fork that specific number of sub-processes.

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

stop()

Stops listening for new connections.

Requests currently in progress may still continue after the server is stopped.

handle_stream(stream, address)

Override to handle a new 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 is configured for SSL, handle_stream may be called before the SSL handshake has completed. Use if you need to verify the client’s certificate or use NPN/ALPN.

在 4.2 版更改: Added the option for this method to be a coroutine.