下面是 “Python3实现域名查询和whois查询功能”的完整攻略。
前置条件
在开始之前,需要安装 whois
和 python-whois
两个库。可以通过以下命令进行安装:
pip install python-whois whois
其中,python-whois
是一个python的whois查询工具库,而whois
则是支持在命令行中查询whois的工具。安装完成后,我们才能开始本攻略的操作。
域名查询功能
有两种方式实现域名查询功能。
使用 Python socket 模块
Python 中可以使用 socket
模块来进行 DNS 解析。
import socket
def get_ip(domain_name):
return socket.gethostbyname(domain_name)
在以上的代码中,使用 socket.gethostbyname()
函数获取域名对应的IP地址。
使用 dnspython 库
dnspython模块是Python中用于解析DNS的级联Python模块。它支持通常使用的所有记录类型,包括A、MX、PTR、TXT等等。
pip install dnspython
安装好 dnspython
库后,可以使用以下代码查询域名对应的IP地址:
import dns.resolver
def get_ip(domain_name):
try:
dns_resolver = dns.resolver.Resolver()
answers = dns_resolver.query(domain_name, 'A')
return str(answers[0])
except Exception as e:
print(e)
在以上代码中,我们使用了 dns.resolver.Resolver()
对象来查询IP地址,解析出来的IP地址是一个IP对象,因此可以使用 str()
函数将其转换为字符串。
Whois 查询功能
现在,我们考虑实现 whois查询功能。
在Python中,whois
模块和python-whois
模块都是用于查询whois信息的库。这里我们选择 python-whois
来实现查询。
以下是查询whois信息的示例代码:
import whois
def get_whois_info(domain_name):
try:
w = whois.whois(domain_name)
return w
except Exception as e:
print(e)
return None
以上代码中,我们首先使用 whois.whois()
函数查询域名的whois信息。查询成功后,返回一个whois信息类的实例。
需要注意的是,有些whois查询结果可能没有返回所有信息,具体信息和输出格式取决于不同whois服务器和域名。我们也需要处理可能出现的异常,以保证程序的稳定性。
示例说明
假设要查询 github.com
的IP地址和whois信息。以下是查询的示例代码。
ip = get_ip('github.com')
if ip:
print('The IP address of github.com is:', ip)
w = get_whois_info('github.com')
if w:
print(w)
输出结果:
The IP address of github.com is: 140.82.118.3
{
"domain_name": [
"GITHUB.COM",
"github.com"
],
"registrar": "MarkMonitor Inc.",
"whois_server": "whois.markmonitor.com",
"referral_url": null,
"updated_date": [
"2018-08-10 09:20:34-0700",
"2018-08-10 02:20:35-0700"
],
"creation_date": [
"2007-10-09 18:20:50-0700",
"2007-10-09T18:20:50Z"
],
"expiration_date": [
"2020-10-09 18:20:50-0700",
"2020-10-09T18:20:50Z"
],
"name_servers": [
"DNS1.P08.NSONE.NET",
"NS-2046.AWSDNS-63.CO.UK",
"NS-637.AWSDNS-15.NET",
"NS-1157.AWSDNS-16.ORG",
"DNS2.P08.NSONE.NET",
"NS-70.AWSDNS-08.COM",
"NS-1763.AWSDNS-28.CO.UK",
"NS-1219.AWSDNS-24.ORG",
"NS-1004.AWSDNS-61.COM",
"NS-2010.AWSDNS-59.CO.UK",
"NS-377.AWSDNS-47.COM",
"NS-539.AWSDNS-03.NET",
"NS-926.AWSDNS-51.NET",
"NS-165.AWSDNS-20.NET",
"NS-862.AWSDNS-43.NET",
"NS-1473.AWSDNS-56.ORG"
],
"status": [
"clientDeleteProhibited https://icann.org/epp#clientDeleteProhibited",
"clientTransferProhibited https://icann.org/epp#clientTransferProhibited",
"clientUpdateProhibited https://icann.org/epp#clientUpdateProhibited",
"serverDeleteProhibited https://icann.org/epp#serverDeleteProhibited",
"serverTransferProhibited https://icann.org/epp#serverTransferProhibited",
"serverUpdateProhibited https://icann.org/epp#serverUpdateProhibited",
"clientRenewProhibited https://icann.org/epp#clientRenewProhibited",
"clientTransferProhibited (https://www.icann.org/epp#clientTransferProhibited)",
"clientUpdateProhibited (https://www.icann.org/epp#clientUpdateProhibited)",
"clientDeleteProhibited (https://www.icann.org/epp#clientDeleteProhibited)",
"serverTransferProhibited (https://www.icann.org/epp#serverTransferProhibited)",
"serverUpdateProhibited (https://www.icann.org/epp#serverUpdateProhibited)",
"serverDeleteProhibited (https://www.icann.org/epp#serverDeleteProhibited)",
"clientRenewProhibited (https://www.icann.org/epp#clientRenewProhibited)"
],
"emails": [
"abusecomplaints@markmonitor.com",
"whoisrequest@markmonitor.com"
],
"dnssec": "unsigned",
"name": null,
"org": "GitHub, Inc.",
"address": null,
"city": null,
"state": "CA",
"zipcode": null,
"country": "US"
}
以上示例代码中,我们首先通过 get_ip()
函数获取 github.com
的IP地址。如果获取成功,则输出IP地址。接着,使用 get_whois_info()
函数获取 github.com
的 whois 信息。如果查询结果不为空,则输出查询结果。
到此,我已经给你讲解了如何实现Python3实现域名查询和whois查询功能,并提供了相应的示例代码,希望对你有所帮助!
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:python3实现域名查询和whois查询功能 - Python技术站