Perform a basic query
The most basic query is a forward query, which looks up an IP address based on a host name.
Getaddrinfo (...) getaddrinfo (host, port [, family, socktype, proto, flags ]) -> list of (family, socktype, PROTO, CANONNAME, SOCKADDR) Resolve host And port into addrinfo struct. #根据给定的参数host/port, the corresponding conversion to a five-tuple that contains the object used to create the socket, # The parameter host is a domain name, given as a string representing an Ipv4/ipv6 address or a none. #参数port如果字符串形式就代表一个服务名, such as "http", "ftp", "email", or a number, or none #参数family为地主族, can be used for af_inet ,af_inet6 ,af_unix. #参数socketype可以为SOCK_STREAM (TCP) or a combination of Sock_dgram (UDP) #参数proto通常为0可以直接忽略 #参数flags为AI_ *, such as ai_numerichost, which affects the return value of a function #附注: Pass NULL to the parameter host,port when the none is established on the C base, by passing. #该函数返回一个五元组 (FAMILY, SOCKTYPE, PROTO, CANONNAME, SOCKADDR), At the same time the fifth parameter sockaddr is also a two-tuple (Address, port)
In [30]: m = socket.getaddrinfo ("www.qq.com", none) in [31]: m The //function returns a list that consists of tuples out[ 31]: [(2, 1, 6, ', (' 14.17.32.211 ', 0)), (2, 2, 17, ", (' 14.17.32.211 ', 0)), (2, 3, 0, ", (' 14.17.32.211 ', 0)), (2, 1, 6, ', (' 59.37.96.63 ', 0)), (2, 2, 17, ', (' 59.37.96.63 ', 0)), (2, 3, 0, ', (' 59.37.96.63 ', 0)), (2, 1, 6, " , (' 14.17.42.40 ', 0)), (2, 2, 17, ', (' 14.17.42.40 ', 0)), (2, 3, 0, ', (' 14.17.42.40 ', 0)), (10, 1, 6, ', (' 240E:FF: F040:28::a ', 0, 0, 0)), (10, 2, 17, ', (' 240e:ff:f040:28::a ', 0, 0, 0)), (10, 3, 0, ', (' 240e:ff:f040:28::a ', 0, 0, 0))] in [32]: m[4][4] //only select IP address out[32]: (' 59.37.96.63 ', 0) in [33]: m[4][4][0] //Note This IP when the string out[33]: ' 59.37.96.63 '
To perform a reverse query, there is a host name for the IP address lookup response:
GETHOSTBYADDR (...) GETHOSTBYADDR (host)--(name, Aliaslist, AddressList) Return The true host name, a list of aliases, and a list o F IP addresses, for a host. The host argument is a string giving a host name or IP number.
Authenticity Check for reverse lookup:
The method of checking is to find out a domain name based on IP first.
Then find an IP list based on the domain name.
Finally check if the IP you are using is in the IP list again.
Code domain name lookup and detection
This article is from "Custom" blog, declined reprint!
Using a domain name query in python