Python implementation of DNS forward query, reverse query example _python

Source: Internet
Author: User
Tags socket domain name server domain server

1.DNS Query process:

Take query www.baidu.com as an example

(1) The computer sends the request of parsing www.baidu.com to the local domain name server
(2) The local domain name server receives the request, first inquires the local cache, if finds the direct return query result, if does not have this record, the local domain name server sends the www.baidu.com request to the root domain name server
(3) When the root domain server receives the request, the server IP address of the. com domain is returned to the local domain name server
(4) The local domain name server connects the. COM server to its request to resolve the domain name www.baidu.com, the. COM server returns the IP address of the baidu.com server to the local DNS server
(5) The local DNS server sends a resolution domain name request to the Baidu.com server, and the baidu.com server returns the www.baidu.com IP address to the local DNS server
(6) The local DNS server returns the IP address of the www.baidu.com to the computer.

2. The corresponding relationship between domain name and IP address:

A domain name can correspond to multiple IP addresses, but at the same time, a domain name can only have an IP address, an IP address can correspond to multiple domain names.

3. Query DNS

Python can implement forward and reverse queries for DNS. The following is the code for the forward query:

Copy Code code as follows:

#!/usr/bin/env python
Import Sys,socket
Result=socket.getaddrinfo (Sys,argv[1],none)
Print Result[0][4]

Because a domain name can have more than one IP address, the results of the above program run two times may be different.

To run the program:

Copy Code code as follows:
./test.py www.baidu.com

The results are:
Copy Code code as follows:
(' 111.13.100.91 ', 80)

Reverse query:

Copy Code code as follows:

#!/usr/bin/env python
Import Sys,socket
Try
RESULT=SOCKET.GETHOSTBYADDR (Sys.argv[1])
Print "hostname is" +result[0]
Except Socket.herror,e:
Print "Can ' t look up"

Running programs

Copy Code code as follows:
./test2 127.0.0.1

The results are:
Copy Code code as follows:
Hostname is localhost

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.