The Domain Name System (DNS) is a distributed database used to replace the host name with an IP address.
There are two reasons for DNS:
(1) It makes it easy for users to remember their names, rather than pure IP addresses;
(2) allow the server to change the IP address, but use the original domain name.
The most basic query in the system is forward query, which searches for IP addresses based on a host name. For example, if you want to download a web page from www.example.com, you must first find the IP address. The query will help you complete this task. It will translate a name into an IP address.
A socket. getaddrinfo () function is defined in Python to complete this forward query. It is defined as follows:
Getaddrinfo (host, Port [, family [, sockettype [, proto [, flags])
The host parameter is the domain name you want to search for. Other parameters are used only when you want to directly pass the result to socket. socket () or socket. Connect.
Below is a small example of forward query:
Import sys, socketresult = socket. getaddrinfo (sys. argv [1], none, 0, socket. sock_stream) Counter = 0; for item in result: Print "%-2D: % s" % (counter, item [4]) Counter + = 1
Running result: