tornado.netutil — Miscellaneous network utilities¶
Miscellaneous network utility code.
tornado.netutil.
bindsockets
(_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 ifthe given address maps to multiple IP addresses, which is most commonfor 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 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()
.flags
is a bitmask of AI* flags togetaddrinfo
, likesocket.AI_PASSIVE | socket.AI_NUMERICHOST
.reuse_port
option setsSO_REUSEPORT
option for every socketin the list. If your platform doesn’t support this option ValueError willbe 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 beraised.
Returns a socket object (not a list of socket objects likebind_sockets
)
tornado.netutil.
addaccept_handler
(_sock, callback, io_loop=None)[源代码]¶
Adds anIOLoop
event handler to accept new connections onsock
.
When a connection is accepted,callback(connection, address)
willbe run (connection
is a socket object, andaddress
is theaddress of the other end of the connection). Note that this signatureis different from thecallback(fd, events)
signature used forIOLoop
handlers.
在 4.1 版更改: Theioloop
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 callssocket.getaddrinfo
). An alternative implementation can bechosen with theResolver.configure
class method:- Resolver.configure('tornado.netutil.ThreadedResolver')
The implementations of this interface included with Tornado are
- tornado.netutil.BlockingResolver
- tornado.netutil.ThreadedResolver
- tornado.netutil.OverrideResolver
- tornado.platform.twisted.TwistedResolver
- tornado.platform.caresresolver.CaresResolverresolve
(host, port, family=0, callback=None)[源代码]¶
Resolves an address.
Thehost
argument is a string which may be a hostname or aliteral IP address.
Returns aFuture
whose result is a list of (family,address) pairs, where address is a tuple suitable to pass tosocket.connect
(i.e. a(host,
pair for IPv4; additional fields may be present forIPv6). If a
port)callback
is passed, it will be run with theresult as an argument when it is complete.
|引发:
|——-
|IOError – if the address cannot be resolved.
在 4.4 版更改: Standardized all implementations to raiseIOError
.
- Resolver.configure('tornado.netutil.ThreadedResolver')
- class
tornado.netutil.
ExecutorResolver
[源代码]¶
Resolver implementation using aconcurrent.futures.Executor
.
Use this instead ofThreadedResolver
when you require additionalcontrol over the executor being used.
The executor will be shut down when the resolver is closed unlesscloseresolver=False
; use this if you want to reuse the sameexecutor elsewhere.
在 4.1 版更改: Theio_loop
argument is deprecated.
- _class
tornado.netutil.
BlockingResolver
[源代码]¶
DefaultResolver
implementation, usingsocket.getaddrinfo
.
TheIOLoop
will be blocked during the resolution, although thecallback will not be run until the nextIOLoop
iteration.
- class
tornado.netutil.
ThreadedResolver
[源代码]¶
Multithreaded non-blockingResolver
implementation.
Requires theconcurrent.futures
package to be installed(available in the standard library since Python 3.2,installable withpip install futures
in older versions).
The thread pool size can be configured with:- Resolver.configure('tornado.netutil.ThreadedResolver',
numthreads=10)
在 3.1 版更改: AllThreadedResolvers
share a single thread pool, whosesize is set by the first one to be created.- Resolver.configure('tornado.netutil.ThreadedResolver',
- _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.
ssloptions_to_context
(_ssl_options)[源代码]¶
Try to convert anssloptions
dictionary to anSSLContext
object.
Thessl_options
dictionary contains keywords to be passed tossl.wrap_socket
. In Python 2.7.9+,ssl.SSLContext
objects canbe used instead. This function converts the dict form to itsSSLContext
equivalent, and may be used when a component whichaccepts both forms needs to upgrade to theSSLContext
versionto use features like SNI or NPN.
tornado.netutil.
ssl_wrap_socket
(_socket, ssl_options, server_hostname=None, **kwargs)[源代码]¶
Returns anssl.SSLSocket
wrapping the given socket.ssl_options
may be either anssl.SSLContext
object or adictionary (as accepted byssl_options_to_context
). Additionalkeyword arguments are passed towrap_socket
(either theSSLContext
method or thessl
module function asappropriate).
原文:
https://tornado-zh-cn.readthedocs.io/zh_CN/latest/netutil.html