### Navigation
- [index](# "General Index")
- [modules](# "Python Module Index") |
- [next](# "tornado.tcpclient — IOStream connection factory") |
- [previous](# "tornado.iostream — Convenient wrappers for non-blocking sockets") |
- [Tornado 4.4.dev1 documentation](#) »
- [Asynchronous networking](#) »
# `tornado.netutil` — Miscellaneous network utilities
Miscellaneous network utility code.
`tornado.netutil.``bind_sockets`(*port*, *address=None*, *family=<AddressFamily.AF_UNSPEC: 0>*, *backlog=128*, *flags=None*, *reuse_port=False*)[[source]](#)
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 either [`socket.AF_INET`](https://docs.python.org/3.4/library/socket.html#socket.AF_INET "(in Python v3.4)") [https://docs.python.org/3.4/library/socket.html#socket.AF_INET]or [`socket.AF_INET6`](https://docs.python.org/3.4/library/socket.html#socket.AF_INET6 "(in Python v3.4)") [https://docs.python.org/3.4/library/socket.html#socket.AF_INET6] to restrict to IPv4 or IPv6 addresses, otherwiseboth will be used if available.
The `backlog` argument has the same meaning as for[`socket.listen()`](https://docs.python.org/3.4/library/socket.html#socket.socket.listen "(in Python v3.4)") [https://docs.python.org/3.4/library/socket.html#socket.socket.listen].
`flags` is a bitmask of AI_* flags to [`getaddrinfo`](https://docs.python.org/3.4/library/socket.html#socket.getaddrinfo "(in Python v3.4)") [https://docs.python.org/3.4/library/socket.html#socket.getaddrinfo], like`socket.AI_PASSIVE | socket.AI_NUMERICHOST`.
`resuse_port` option sets `SO_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*)[[source]](#)
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 like[`bind_sockets`](# "tornado.netutil.bind_sockets"))
`tornado.netutil.``add_accept_handler`(*sock*, *callback*, *io_loop=None*)[[source]](#)
Adds an [`IOLoop`](# "tornado.ioloop.IOLoop") event handler to accept new connections on `sock`.
When a connection is accepted, `callback(connection, address)` willbe run (`connection` is a socket object, and `address` is theaddress of the other end of the connection). Note that this signatureis different from the `callback(fd, events)` signature used for[`IOLoop`](# "tornado.ioloop.IOLoop") handlers.
Changed in version 4.1: The `io_loop` argument is deprecated.
`tornado.netutil.``is_valid_ip`(*ip*)[[source]](#)
Returns true if the given string is a well-formed IP address.
Supports IPv4 and IPv6.
*class *`tornado.netutil.``Resolver`[[source]](#)
Configurable asynchronous DNS resolver interface.
By default, a blocking implementation is used (which simply calls[`socket.getaddrinfo`](https://docs.python.org/3.4/library/socket.html#socket.getaddrinfo "(in Python v3.4)") [https://docs.python.org/3.4/library/socket.html#socket.getaddrinfo]). An alternative implementation can bechosen with the [`Resolver.configure`](# "tornado.util.Configurable.configure")class method:
~~~
Resolver.configure('tornado.netutil.ThreadedResolver')
~~~
The implementations of this interface included with Tornado are
- [`tornado.netutil.BlockingResolver`](# "tornado.netutil.BlockingResolver")
- [`tornado.netutil.ThreadedResolver`](# "tornado.netutil.ThreadedResolver")
- [`tornado.netutil.OverrideResolver`](# "tornado.netutil.OverrideResolver")
- [`tornado.platform.twisted.TwistedResolver`](# "tornado.platform.twisted.TwistedResolver")
- [`tornado.platform.caresresolver.CaresResolver`](# "tornado.platform.caresresolver.CaresResolver")
`resolve`(*host*, *port*, *family=<AddressFamily.AF_UNSPEC: 0>*, *callback=None*)[[source]](#)
Resolves an address.
The `host` argument is a string which may be a hostname or aliteral IP address.
Returns a [`Future`](# "tornado.concurrent.Future") whose result is a list of (family,address) pairs, where address is a tuple suitable to pass to[`socket.connect`](https://docs.python.org/3.4/library/socket.html#socket.socket.connect "(in Python v3.4)") [https://docs.python.org/3.4/library/socket.html#socket.socket.connect] (i.e. a `(host,port)` pair for IPv4; additional fields may be present forIPv6). If a `callback` is passed, it will be run with theresult as an argument when it is complete.
`close`()[[source]](#)
Closes the [`Resolver`](# "tornado.netutil.Resolver"), freeing any resources used.
New in version 3.1.
*class *`tornado.netutil.``ExecutorResolver`[[source]](#)
Resolver implementation using a [`concurrent.futures.Executor`](https://docs.python.org/3.4/library/concurrent.futures.html#concurrent.futures.Executor "(in Python v3.4)") [https://docs.python.org/3.4/library/concurrent.futures.html#concurrent.futures.Executor].
Use this instead of [`ThreadedResolver`](# "tornado.netutil.ThreadedResolver") when you require additionalcontrol 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 sameexecutor elsewhere.
Changed in version 4.1: The `io_loop` argument is deprecated.
*class *`tornado.netutil.``BlockingResolver`[[source]](#)
Default [`Resolver`](# "tornado.netutil.Resolver") implementation, using [`socket.getaddrinfo`](https://docs.python.org/3.4/library/socket.html#socket.getaddrinfo "(in Python v3.4)") [https://docs.python.org/3.4/library/socket.html#socket.getaddrinfo].
The [`IOLoop`](# "tornado.ioloop.IOLoop") will be blocked during the resolution, although thecallback will not be run until the next [`IOLoop`](# "tornado.ioloop.IOLoop") iteration.
*class *`tornado.netutil.``ThreadedResolver`[[source]](#)
Multithreaded non-blocking [`Resolver`](# "tornado.netutil.Resolver") implementation.
Requires the [`concurrent.futures`](https://docs.python.org/3.4/library/concurrent.futures.html#module-concurrent.futures "(in Python v3.4)") [https://docs.python.org/3.4/library/concurrent.futures.html#module-concurrent.futures] 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)
~~~
Changed in version 3.1: All `ThreadedResolvers` share a single thread pool, whosesize is set by the first one to be created.
*class *`tornado.netutil.``OverrideResolver`[[source]](#)
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*)[[source]](#)
Try to convert an `ssl_options` dictionary to an[`SSLContext`](https://docs.python.org/3.4/library/ssl.html#ssl.SSLContext "(in Python v3.4)") [https://docs.python.org/3.4/library/ssl.html#ssl.SSLContext] object.
The `ssl_options` dictionary contains keywords to be passed to[`ssl.wrap_socket`](https://docs.python.org/3.4/library/ssl.html#ssl.wrap_socket "(in Python v3.4)") [https://docs.python.org/3.4/library/ssl.html#ssl.wrap_socket]. In Python 2.7.9+, [`ssl.SSLContext`](https://docs.python.org/3.4/library/ssl.html#ssl.SSLContext "(in Python v3.4)") [https://docs.python.org/3.4/library/ssl.html#ssl.SSLContext] objects canbe used instead. This function converts the dict form to its[`SSLContext`](https://docs.python.org/3.4/library/ssl.html#ssl.SSLContext "(in Python v3.4)") [https://docs.python.org/3.4/library/ssl.html#ssl.SSLContext] equivalent, and may be used when a component whichaccepts both forms needs to upgrade to the [`SSLContext`](https://docs.python.org/3.4/library/ssl.html#ssl.SSLContext "(in Python v3.4)") [https://docs.python.org/3.4/library/ssl.html#ssl.SSLContext] versionto use features like SNI or NPN.
`tornado.netutil.``ssl_wrap_socket`(*socket*, *ssl_options*, *server_hostname=None*, ***kwargs*)[[source]](#)
Returns an `ssl.SSLSocket` wrapping the given socket.
`ssl_options` may be either an [`ssl.SSLContext`](https://docs.python.org/3.4/library/ssl.html#ssl.SSLContext "(in Python v3.4)") [https://docs.python.org/3.4/library/ssl.html#ssl.SSLContext] object or adictionary (as accepted by [`ssl_options_to_context`](# "tornado.netutil.ssl_options_to_context")). Additionalkeyword arguments are passed to `wrap_socket` (either the[`SSLContext`](https://docs.python.org/3.4/library/ssl.html#ssl.SSLContext "(in Python v3.4)") [https://docs.python.org/3.4/library/ssl.html#ssl.SSLContext] method or the [`ssl`](https://docs.python.org/3.4/library/ssl.html#module-ssl "(in Python v3.4)") [https://docs.python.org/3.4/library/ssl.html#module-ssl] module function asappropriate).
© Copyright 2009-2016, The Tornado Authors. Created using [Sphinx](http://sphinx-doc.org/) 1.3.5.
- User's guide
- Introduction
- Asynchronous and non-Blocking I/O
- Coroutines
- Queue example - a concurrent web spider
- Structure of a Tornado web application
- Templates and UI
- Authentication and security
- Running and deploying
- Web framework
- tornado.web — RequestHandler and Application classes
- tornado.template — Flexible output generation
- tornado.escape — Escaping and string manipulation
- tornado.locale — Internationalization support
- tornado.websocket — Bidirectional communication to the browser
- HTTP servers and clients
- tornado.httpserver — Non-blocking HTTP server
- tornado.httpclient — Asynchronous HTTP client
- tornado.httputil — Manipulate HTTP headers and URLs
- tornado.http1connection – HTTP/1.x client/server implementation
- Asynchronous networking
- tornado.ioloop — Main event loop
- tornado.iostream — Convenient wrappers for non-blocking sockets
- tornado.netutil — Miscellaneous network utilities
- tornado.tcpclient — IOStream connection factory
- tornado.tcpserver — Basic IOStream-based TCP server
- Coroutines and concurrency
- tornado.gen — Simplify asynchronous code
- tornado.concurrent — Work with threads and futures
- tornado.locks – Synchronization primitives
- tornado.queues – Queues for coroutines
- tornado.process — Utilities for multiple processes
- Integration with other services
- tornado.auth — Third-party login with OpenID and OAuth
- tornado.wsgi — Interoperability with other Python frameworks and servers
- tornado.platform.asyncio — Bridge between asyncio and Tornado
- tornado.platform.caresresolver — Asynchronous DNS Resolver using C-Ares
- tornado.platform.twisted — Bridges between Twisted and Tornado
- Utilities
- tornado.autoreload — Automatically detect code changes in development
- tornado.log — Logging support
- tornado.options — Command-line parsing
- tornado.stack_context — Exception handling across asynchronous callbacks
- tornado.testing — Unit testing support for asynchronous code
- tornado.util — General-purpose utilities
- Frequently Asked Questions
- Release notes
- What's new in Tornado 4.3
- What's new in Tornado 4.2.1
- What's new in Tornado 4.2
- What's new in Tornado 4.1
- What's new in Tornado 4.0.2
- What's new in Tornado 4.0.1
- What's new in Tornado 4.0
- What's new in Tornado 3.2.2
- What's new in Tornado 3.2.1
- What's new in Tornado 3.2
- What's new in Tornado 3.1.1
- What's new in Tornado 3.1
- What's new in Tornado 3.0.2
- What's new in Tornado 3.0.1
- What's new in Tornado 3.0
- What's new in Tornado 2.4.1
- What's new in Tornado 2.4
- What's new in Tornado 2.3
- What's new in Tornado 2.2.1
- What's new in Tornado 2.2
- What's new in Tornado 2.1.1
- What's new in Tornado 2.1
- What's new in Tornado 2.0
- What's new in Tornado 1.2.1
- What's new in Tornado 1.2
- What's new in Tornado 1.1.1
- What's new in Tornado 1.1
- What's new in Tornado 1.0.1
- What's new in Tornado 1.0