def get_ip(domain):
try:
ip = socket.gethostbyname('blog.65.hk')
print(fThe IP address of blog.65.hk is: {ip})
except socket.gaierror:
print(fUnable to retrieve IP address for {domain})
get_ip('blog.65.hk')
或者使用`dnspython`库(需要安装,`pip install dnspython`):
python
import dns.resolver
def get_ip(domain):
try:
answers = dns.resolver.resolve(domain, 'A')
for rdata in answers:
print(fThe IP address of {domain} is: {rdata.to_text()})
except Exception as e:
print(fAn error occurred: {e})