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]

沒有留言:

張貼留言

[Linux 文章收集] Linux / Unix: Check Last Time User Logged In On The System

Source From  Here   Question   I am a new Unix system admin. How do I find ouw who has recently use the Linux or Unix-like server?  Which te...