tornado.netutil — Miscellaneous network utilities¶
Miscellaneous network utility code.
bind_sockets(port, address=None, family=0, backlog=128, flags=None)¶
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
socket.AF_INET6to restrict to IPv4 or IPv6 addresses, otherwise both will be used if available.
backlogargument has the same meaning as for
flagsis a bitmask of AI_* flags to
socket.AI_PASSIVE | socket.AI_NUMERICHOST.
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
add_accept_handler(sock, callback, io_loop=None)¶
IOLoopevent handler to accept new connections on
When a connection is accepted,
callback(connection, address)will be run (
connectionis a socket object, and
addressis the address of the other end of the connection). Note that this signature is different from the
callback(fd, events)signature used for
Returns true if the given string is a well-formed IP address.
Supports IPv4 and IPv6.
Configurable asynchronous DNS resolver interface.
The implementations of this interface included with Tornado are
resolve(host, port, family=0, callback=None)¶
Resolves an address.
hostargument is a string which may be a hostname or a literal IP address.
Futurewhose result is a list of (family, address) pairs, where address is a tuple suitable to pass to
(host, port)pair for IPv4; additional fields may be present for IPv6). If a
callbackis passed, it will be run with the result as an argument when it is complete.
Resolver implementation using a
Use this instead of
ThreadedResolverwhen 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.
concurrent.futurespackage to be installed (available in the standard library since Python 3.2, installable with
pip install futuresin older versions).
The thread pool size can be configured with:
Changed in version 3.1: All
ThreadedResolversshare a single thread pool, whose size is set by the first one to be created.
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.
Try to convert an
ssl_optionsdictionary to an
ssl_optionsdictionary contains keywords to be passed to
ssl.wrap_socket. In Python 3.2+,
ssl.SSLContextobjects can be used instead. This function converts the dict form to its
SSLContextequivalent, and may be used when a component which accepts both forms needs to upgrade to the
SSLContextversion to use features like SNI or NPN.
ssl_wrap_socket(socket, ssl_options, server_hostname=None, **kwargs)¶
ssl.SSLSocketwrapping the given socket.
ssl_optionsmay be either a dictionary (as accepted by
ssl_options_to_context) or an
ssl.SSLContextobject. Additional keyword arguments are passed to
SSLContextmethod or the
sslmodule function as appropriate).