Utilities in Tornado for asynchronous network operations

The tornado.netutil module includes several functions that are quite useful for both clients and servers. The use of some of these functions are commented as follows:

>>> from tornado import netutil

>>> sockets = netutil.bind_sockets(8080)

>>> sockets

[<socket.socket fd=1108, family=AddressFamily.AF_INET6, type=SocketKind.SOCK_STREAM, proto=0, laddr=('::', 8080, 0, 0)>, <socket.socket fd=1112, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=0, laddr=('0.0.0.0', 8080)>]

>>> netutil.is_valid_ip('127.0.0.1')
True

>>> netutil.is_valid_ip('::1')
True

>>> netutil.is_valid_ip('::11111')
False

>>> dnsResolver = netutil.Resolver()

>>> dnsResolver
<tornado.netutil.DefaultExecutorResolver object at 0x0341FD10>

>>> dnsResolver.resolve('www.packtpub.com',80)

<Future pending cb=[_make_coroutine_wrapper.<locals>.wrapper.<locals>.<lambda>()>

The bind_sockets function is responsible for creating the sockets in all of the available network interfaces and returns a list with each of the references that were created.

The is_valid_ip function validates whether an IPv4 or IPv6 address is valid or not.

Finally, the Resolver class allows you to configure several types of resolvers for blocking and non-blocking DNS requests. The default resolver is tornado.netutil.DefaultExecutorResolver.

For more information about the utilities that are available in Tornado, it is recommended to review the documentation, which can be found at http://tornado.readthedocs.org/en/latest/netutil.html.

In this section, we have reviewed the Tornado framework for creating asynchronous and non-blocking systems. In the following section, we are going to review the Twisted framework for developing asynchronous applications using an event-driven network engine.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset