2018年7月5日 星期四

[ Python 常見問題 ] How to resolve DNS in Python?

Source From Here 
Question 
As title. In Linux shell, we resolve domain this way: 
# dig +short www.google.com
172.217.24.4

# nslookup www.google.com
Server: DNS_IP
Address: DNS_IP#53

Non-authoritative answer:
Name: www.google.com
Address: 172.217.24.4


# host www.google.com
www.google.com has address 172.217.24.4
www.google.com has IPv6 address 2404:6800:4008:800::2004

How to get IP of domain name in Python script? 

How-To 
API:gethostbyname_ex is what you need: 
Translate a host name to IPv4 address format, extended interface. Return a triple (hostname, aliaslist, ipaddrlist) where hostname is the primary host name responding to the given ip_address, aliaslist is a (possibly empty) list of alternative host names for the same address, and ipaddrlist is a list of IPv4 addresses for the same interface on the same host (often but not always a single address).

One example as below: 
>>> socket.gethostbyname_ex('www.google.com')
('www.google.com', [], ['172.217.24.4'])

Supplement 
Linux command to translate DomainName to IP [closed]

沒有留言:

張貼留言

[Git 常見問題] error: The following untracked working tree files would be overwritten by merge

  Source From  Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 #   git clean -d -fx 方案2: 今天在服务器上  gi...