我们需要将要查询的域名存储在一个文本文件中,每个域名占一行。编写Python脚本如下:
import dns.resolver
import requests
with open('domains.txt', 'r') as f:
domains = [line.strip() for line in f]
for domain in domains:
try:
# 域名解析
answers = dns.resolver.resolve(domain, 'A')
ip_address = str(answers[0])
# 获取域名的HTTP状态码
response = requests.get(f'http://{domain}')
status_code = response.status_code
print(f'{domain}: IP地址 - {ip_address}, HTTP状态码 - {status_code}')
except:
print(f'{domain}: 查询失败')
该脚本会依次读取文件中的域名,使用dnspython库进行DNS解析获取IP地址,使用requests库发送HTTP请求获取域名的状态码。将结果打印出来。
将上述脚本保存为一个Python文件,例如 domain_checker.py
,在命令行中执行:
python domain_checker.py
脚本将自动读取 domains.txt
文件中的域名,并批量查询它们的IP地址和HTTP状态码。