Working with dnspython

The IP address can be translated into human-readable strings called domain names. DNS is a big topic in the world of networking. In this section, we will create a DNS client in Python, and see how this client will talk to the server using Wireshark.

A few DNS client libraries are available from PyPI. We will focus on the dnspython library, which is available at http://www.dnspython.org.

You can install this library by using either the easy_install command or the pip command:

$ pip install dnspython

In this practical example, we will use dnspython to execute queries on several types of DNS records, such as IPv4 (A), IPv6 (AAAA), name servers (NS), and mail exchange (MX).

The main utility of dnspython regarding other DNS query tools, such as dig, fierce, or nslookup, is that you can control the result of queries from Python, and then that information can be used for other purposes in a script.

You can also install it from its source code, which is available on its official website: http://www.dnspython.org.

Now, we are going to review some interesting queries, such as the examples that appear at http://www.dnspython.org/examples.html.

Making a simple query regarding the IP address of a host is very simple. You can use the dns.resolver submodule, as follows. You can find the following code in the dns_basic.py file:

import dns.resolver
answers = dns.resolver.query('dnspython.org', 'A')
for rdata in answers:
print('IP', rdata.to_text())
..................Content has been hidden....................

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