python 查询IP上绑定的域名
要查询一个IP上绑定的域名,可以使用socket库中的gethostbyaddr函数。以下是一个示例代码:
import socket
def get_domains_by_ip(ip):
try:
hostname = socket.gethostbyaddr(ip)[0]
return hostname
except socket.herror:
return "No domain found for this IP"
ip = "127.0.0.1"
domains = get_domains_by_ip(ip)
print(domains)
在上面的代码中,我们定义了一个get_domains_by_ip函数,它接受一个IP地址作为参数。该函数使用socket.gethostbyaddr函数查询IP对应的域名,并返回域名。如果没有找到域名,函数将返回"No domain found for this IP"。
你可以将要查询的IP地址传递给get_domains_by_ip函数,然后打印返回的域名。在上面的示例中,我们查询了本地回环地址"127.0.0.1",你可以替换为你要查询的IP地址。
原文地址: http://www.cveoy.top/t/topic/h19Z 著作权归作者所有。请勿转载和采集!