tornado.netutil — Miscellaneous network utilities
Miscellaneous network utility code.
tornado.netutil.
bind_sockets
(port, address=None, family=0, backlog=128, flags=None, reuse_port=False)Creates listening sockets bound to the given port and address.
Returns a list of socket objects (multiple sockets are returned if the given address maps to multiple IP addresses, which is most common for mixed IPv4 and IPv6 use).
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 .flags
is a bitmask of AI_* flags to , likesocket.AI_PASSIVE | socket.AI_NUMERICHOST
.reuse_port
option setsSO_REUSEPORT
option for every socket in the list. If your platform doesn’t support this option ValueError will be raised.
tornado.netutil.
bind_unix_socket
(file, mode=384, backlog=128)Creates a listening unix socket.
If a socket with the given name already exists, it will be deleted. If any other file with that name exists, an exception will be raised.
Returns a socket object (not a list of socket objects like )
tornado.netutil.
add_accept_handler
(sock, callback, io_loop=None)Adds an event handler to accept new connections on
sock
.When a connection is accepted,
callback(connection, address)
will be run (connection
is a socket object, andaddress
is the address of the other end of the connection). Note that this signature is different from thecallback(fd, events)
signature used for handlers.在 4.1 版更改: The
io_loop
argument is deprecated.
tornado.netutil.
is_valid_ip
(ip)Returns true if the given string is a well-formed IP address.
Supports IPv4 and IPv6.
- class
tornado.netutil.
Resolver
Configurable asynchronous DNS resolver interface.
By default, a blocking implementation is used (which simply calls ). An alternative implementation can be chosen with the class method:
Resolver.configure('tornado.netutil.ThreadedResolver')
The implementations of this interface included with Tornado are
resolve
(host, port, family=0, callback=None)Resolves an address.
The
host
argument is a string which may be a hostname or a literal IP address.Returns a whose result is a list of (family, address) pairs, where address is a tuple suitable to pass to (i.e. a
(host, port)
pair for IPv4; additional fields may be present for IPv6). If acallback
is passed, it will be run with the result as an argument when it is complete.引发: – if the address cannot be resolved. 在 4.4 版更改: Standardized all implementations to raise .
close
()Closes the , freeing any resources used.
3.1 新版功能.
- class
tornado.netutil.
ExecutorResolver
Resolver implementation using a .
Use this instead of when you require additional control over the executor being used.
The executor will be shut down when the resolver is closed unless
close_resolver=False
; use this if you want to reuse the same executor elsewhere.在 4.1 版更改: The
io_loop
argument is deprecated.
- class
tornado.netutil.
BlockingResolver
Default implementation, using .
The will be blocked during the resolution, although the callback will not be run until the next iteration.
- class
tornado.netutil.
ThreadedResolver
Multithreaded non-blocking implementation.
Requires the package to be installed (available in the standard library since Python 3.2, installable with
pip install futures
in older versions).The thread pool size can be configured with:
Resolver.configure('tornado.netutil.ThreadedResolver', num_threads=10)
在 3.1 版更改: All
ThreadedResolvers
share a single thread pool, whose size is set by the first one to be created.
- class
tornado.netutil.
OverrideResolver
Wraps a resolver with a mapping of overrides.
This can be used to make local DNS changes (e.g. for testing) without modifying system-wide settings.
The mapping can contain either host strings or host-port pairs.
tornado.netutil.
ssl_options_to_context
(ssl_options)Try to convert an
ssl_options
dictionary to an object.The
ssl_options
dictionary contains keywords to be passed to . In Python 2.7.9+, objects can be used instead. This function converts the dict form to its equivalent, and may be used when a component which accepts both forms needs to upgrade to the version to use features like SNI or NPN.
tornado.netutil.
ssl_wrap_socket
(socket, ssl_options, server_hostname=None, **kwargs)Returns an
ssl.SSLSocket
wrapping the given socket.ssl_options
may be either an object or a dictionary (as accepted by ). Additional keyword arguments are passed towrap_socket
(either the method or the module function as appropriate).