欢迎访问宙启技术站
智能推送

手把手教你使用Python中的whois()函数进行域名查询

发布时间:2024-01-13 05:34:38

Python中的whois()函数是一个用于查询域名注册信息的函数,可以通过域名获取注册商、注册时间、过期时间、DNS服务器等相关信息。下面是一个使用whois()函数进行域名查询的示例:

首先需要确保你的Python环境已经安装了python-whois库,可以使用以下命令进行安装:

pip install python-whois

然后,可以按照以下步骤使用whois()函数进行域名查询:

1. 导入所需模块:

import whois

2. 使用域名作为参数调用whois()函数进行查询:

domain = "example.com"
result = whois.whois(domain)

3. 获取域名的注册商信息:

registrar = result.registrar
print("注册商: ", registrar)

4. 获取域名的注册时间:

registration_date = result.creation_date
print("注册时间: ", registration_date)

5. 获取域名的过期时间:

expiration_date = result.expiration_date
print("过期时间: ", expiration_date)

6. 获取域名的DNS服务器:

dns_servers = result.name_servers
print("DNS服务器: ", dns_servers)

完整的代码示例:

import whois

def domain_info(domain):
    try:
        result = whois.whois(domain)

        registrar = result.registrar
        print("注册商: ", registrar)

        registration_date = result.creation_date
        print("注册时间: ", registration_date)

        expiration_date = result.expiration_date
        print("过期时间: ", expiration_date)

        dns_servers = result.name_servers
        print("DNS服务器: ", dns_servers)
    except Exception as e:
        print("查询失败: ", str(e))

# 使用示例
domain_info("example.com")

运行上述代码,将会输出类似以下的查询结果:

注册商:  NameCheap, Inc.
注册时间:  1995-08-14 04:00:00
过期时间:  2023-08-13 04:00:00
DNS服务器:  ['dns1.registrar-servers.com', 'dns2.registrar-servers.com']

这个例子展示了如何使用Python中的whois()函数进行域名查询,并获取域名的注册商、注册时间、过期时间和DNS服务器等相关信息。可以根据自己的需求使用更多的whois()函数返回的属性。