由ip查域名:
(family, type, proto, canonname, sockaddr)=socket.getaddrinfo(host, port, family=0, type=0, proto=0, flags=0)
#-*- coding='gb2312' -*-
import socket
if __name__ == '__main__':
result=socket.getaddrinfo("www.baidu.com",None)
for item in result:
print(item[4])
('115.239.211.110', 0)
('115.239.210.27', 0)
由域名查ip:
(hostname, aliaslist, ipaddrlist)=socket.gethostbyaddr(ip_address)
#-*- coding='gb2312' -*-
import socket
if __name__ == '__main__':
try:
result=socket.gethostbyaddr("127.0.0.1")
print(result[0])
except socket.herror:
print("error")