76 lines
3.2 KiB
Python
76 lines
3.2 KiB
Python
# -*- coding: UTF-8 -*-
|
||
'''
|
||
@Project :domainScanDemo
|
||
@File :register.py
|
||
@IDE :PyCharm
|
||
@Author :梦伴
|
||
@Date :2026/3/27 22:55
|
||
@explain : 域名注册状态检测工具
|
||
'''
|
||
|
||
from datetime import datetime, timezone, timedelta # 日期时间处理,用于时区转换
|
||
|
||
import requests # HTTP请求库
|
||
from loguru import logger # 日志记录
|
||
|
||
|
||
def check_register(domain: str, postfix: str = 'com',proxies: dict = None):
|
||
'''
|
||
检测注册状态
|
||
:param domain: 待检测域名(不包含后缀)
|
||
:param postfix: 域名后缀com、net(默认com)
|
||
:param proxies: 代理(默认None)
|
||
:return: 注册状态(2:可注册,3:已注册,-1:检测失败,过期时间(格式:YYYY-MM-DD HH:MM:SS)为空)
|
||
'''
|
||
url = f'https://rdap.verisign.com/{postfix}/v1/domain/{domain}' # 构建RDAP查询URL
|
||
# url = f'https://www.baidu.com' # 构建RDAP查询URL
|
||
|
||
# 添加重试机制
|
||
max_retries = 3
|
||
for retry in range(max_retries):
|
||
try:
|
||
response = requests.get(url, timeout=20, proxies=proxies) # 发送GET请求,设置超时5秒
|
||
if response.status_code == 200: # HTTP 200表示域名已注册
|
||
json_data = response.json() # 解析JSON响应
|
||
for item in json_data['events']: # 遍历事件列表
|
||
if item['eventAction'] == 'expiration': # 如果是过期时间事件
|
||
utc_time = datetime.fromisoformat(item['eventDate'].replace('Z', '+00:00')) # 解析UTC时间
|
||
beijing_tz = timezone(timedelta(hours=8)) # 创建北京时区(UTC+8)
|
||
beijing_time = utc_time.astimezone(beijing_tz) # 将UTC时间转换为北京时间
|
||
return 3, beijing_time.strftime("%Y-%m-%d %H:%M:%S") # 返回已注册状态和过期时间
|
||
|
||
elif response.status_code == 404: # HTTP 404表示域名不存在,可注册
|
||
# 可注册状态
|
||
return 2, '' # 返回可注册状态,过期时间为空
|
||
logger.error(response.status_code)
|
||
except requests.exceptions.RequestException as e:
|
||
# logger.warning(f"注册状态检测第{retry+1}次失败: {domain}, 错误: {e}")
|
||
if retry < max_retries - 1:
|
||
import time
|
||
time.sleep(2) # 等待2秒后重试
|
||
proxies=None
|
||
else:
|
||
# 达到最大重试次数,返回检测失败
|
||
logger.error(f"注册状态检测失败: {domain}, 已达到最大重试次数")
|
||
return -1, '' # -1表示检测失败,过期时间为空
|
||
|
||
|
||
|
||
|
||
# def get_proxy():
|
||
#
|
||
# url='http://api.ch12361.com/getProxy.php?group=A&count=1'
|
||
# response = requests.get(url).json()
|
||
# username=response['username']
|
||
# password=response['password']
|
||
# ip=response['ip']
|
||
# port=response['port']
|
||
#
|
||
# proxy_url = f"http://{username}:{password}@{ip}:{port}"
|
||
# logger.info(proxy_url)
|
||
# return {'http':proxy_url,'https':proxy_url}
|
||
#
|
||
# t=datetime.now()
|
||
# logger.info(check_register('75az.com',proxies=get_proxy())) # 测试查询nnsk.com域名的注册状态
|
||
# logger.info(datetime.now()-t)
|