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:
The results are:
Copy Code code as follows:
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:
The results are:
Copy Code code as follows: