1.原因
因为需要梳理公网域名和DNS解析对应关系,因此制作此小脚本。
2.前期准备
在使用脚本前,确定已安装了requests
模块和dnspython
模块(在终端界面哦)
pip install requests dnspython
3.完整的请求代码
from urllib.parse import urlparse
import csv
import dns.resolver
import requests
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
'Dnt': '1',
'Sec-Fetch-Dest': 'document',
'Sec-Fetch-Mode': 'navigate',
'Sec-Fetch-Site': 'same-origin',
'Sec-Fetch-User': '?1',
'Upgrade-Insecure-Requests': '1',
'Sec-Ch-Ua': '"Not_A Brand";v="8", "Chromium";v="120", "Google Chrome";v="120"',
'sec-ch-ua-mobile': '?0',
'sec-ch-ua-platform': '"Windows"'
}
# 读取包含域名的文件
with open('D:\\python\\Domains.txt', 'r', encoding='utf-8') as file:
urls = file.read().splitlines() # 字符串方法:按照行分隔符(换行),装入列表urls里
# 打开文件以写入CSV输出
with open('D:\\python\\域名检查结果.csv', 'w', newline='', encoding='gbk') as csv_file:
csv_writer = csv.writer(csv_file)
# 写入CSV的标题行
csv_writer.writerow(['URL', '域名', '公网DNS解析IP', '服务器响应'])
for url in urls:
print(url)
# 解析URL
parsed_url = urlparse(url)
# 获取域名
domain = parsed_url.netloc.split(':')[0]
# 执行DNS解析以获取域名解析地址(A记录)
try:
answers = dns.resolver.resolve(domain, 'A')
ip_addresses = '-'.join(str(answer.address) for answer in answers) # 使用'-'连接所有IP地址
try:
# 设置超时时间为10秒的url请求
response = requests.get(url, headers=headers, timeout=10)
status_code = response.status_code
except requests.RequestException as e:
status_code = f"网站无响应:{e}" # 如果请求失败,写入错误e信息
# 写入CSV的一行数据
csv_writer.writerow([url, domain, ip_addresses, status_code])
except dns.resolver.NoAnswer:
csv_writer.writerow([url, domain, "DNS无响应", "N/A"]) # DNS无响应时写入N/A
except dns.resolver.NXDOMAIN:
csv_writer.writerow([url, domain, "A域名不存在", "N/A"]) # 域名不存在时写入N/A
except Exception as e:
csv_writer.writerow([url, domain, "异常", e]) # 其他错误时写入错误信息
4.脚本用法:
把Domains.txt文件,存放在D:\Python\
文件夹里(脚本中指定了具体位置),其中每一行存放一个URL,像这样(注意格式,有没带端口都可以)
执行过程会有显示:
执行结果如下图:
5.异常
如果有异常也会在Excle中记录,抛异常
遇到这种是:超时
网站无响应: HTTPSConnectionPool(host='xxxxx.xxxxx.com', port=443): Max retries exceeded with url: / (Caused by ConnectTimeoutError(<urllib3.connection.HTTPSConnection object at 0x0000012ABAF899D0>, 'Connection to xxxxx.xxxxx.com timed out. (connect timeout=10)'))
遇到这种是:你请求的格式不合要求,人家不跟你握手,但网站是存在。主动拒绝了你。
网站无响应: HTTPConnectionPool(host='localhost', port=80): Max retries exceeded with url: /h5/ (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x0000012ABAF775D0>: Failed to establish a new connection: [WinError 10061] 由于目标计算机积极拒绝,无法连接。'))
遇到这种是:证书错误,但网站是存在。有响应的。
网站无响应: HTTPSConnectionPool(host='xxxxx.xxxxx.com', port=443): Max retries exceeded with url: / (Caused by SSLError(SSLCertVerificationError(1, "[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: Hostname mismatch, certificate is not valid for 'xxxxx.xxxxx.com'. (_ssl.c:1002)")))
标签:url,writer,域名,DNS,公网,csv,xxxxx From: https://www.cnblogs.com/Magiclala/p/18057207