详细介绍:Python 2025年10月最新:多平台域名/小程序封禁检测工具

介绍

本文提供基于Python的完整域名和小程序状态检测解决方案,支持QQ、微信、抖音域名检测以及微信小程序状态检测。所有代码均为2025年10月最新版本,接口稳定可靠。

完整代码实现

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
2025年10月最新Python域名/小程序状态检测工具
作者: Python开发者
版本: v1.0.0
日期: 2025-10-01
"""
import requests
import json
from typing import Dict, Any
from urllib.parse import quote
class DomainChecker:
    """
    多平台域名封禁检测工具类
    支持QQ、微信、抖音域名检测和微信小程序状态检测
    """
    def __init__(self, timeout: int = 10):
        """
        初始化检测器
        Args:
            timeout: 请求超时时间(秒)
        """
        self.timeout = timeout
        self.headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36',
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        }
    def check_qq_domain(self, domain: str) -> Dict[str, Any]:
        """
        检测QQ域名状态
        Args:
            domain: 要检测的域名
        Returns:
            dict: 检测结果 {"status":"1","message":"域名正常"}
            status为1是正常,为0是被封
        """
        if not domain or not self._is_valid_domain(domain):
            return {"status": "0", "message": "域名格式不正确"}
        try:
            # 构建API请求URL
            api_url = f"https://api.wxapi.work/qq/api.Python?url={quote(domain)}"
            # 发送请求
            response = requests.get(api_url, headers=self.headers, timeout=self.timeout)
            response.raise_for_status()
            # 解析响应数据
            result = response.json()
            # 根据API响应格式处理结果
            if result.get('status') == 2:
                return {"status": "1", "message": "域名正常"}
            else:
                return {"status": "0", "message": "域名被封禁"}
        except requests.exceptions.RequestException as e:
            return {"status": "0", "message": f"网络请求失败: {str(e)}"}
        except json.JSONDecodeError:
            return {"status": "0", "message": "API响应数据解析失败"}
        except Exception as e:
            return {"status": "0", "message": f"检测异常: {str(e)}"}
    def check_wechat_domain(self, domain: str) -> Dict[str, Any]:
        """
        检测微信域名状态
        Args:
            domain: 要检测的域名
        Returns:
            dict: 检测结果 {"status":"1","message":"域名正常"}
            status为1是被拦截,为0是被封,2是正常
        """
        if not domain or not self._is_valid_domain(domain):
            return {"status": "0", "message": "域名格式不正确"}
        try:
            # 构建API请求URL
            api_url = f"https://api.wxapi.work/wechat/api.Python?url={quote(domain)}"
            # 发送请求
            response = requests.get(api_url, headers=self.headers, timeout=self.timeout)
            response.raise_for_status()
            # 解析响应数据
            result = response.json()
            # 状态映射处理
            status_mapping = {
                2: {"status": "2", "message": "域名正常"},
                1: {"status": "1", "message": "域名被拦截"},
                0: {"status": "0", "message": "域名被封禁"}
            }
            api_status = result.get('status', 0)
            return status_mapping.get(api_status, {"status": "0", "message": "检测失败"})
        except requests.exceptions.RequestException as e:
            return {"status": "0", "message": f"网络请求失败: {str(e)}"}
        except json.JSONDecodeError:
            return {"status": "0", "message": "API响应数据解析失败"}
        except Exception as e:
            return {"status": "0", "message": f"检测异常: {str(e)}"}
    def check_douyin_domain(self, domain: str) -> Dict[str, Any]:
        """
        检测抖音域名状态
        Args:
            domain: 要检测的域名
        Returns:
            dict: 检测结果 {"status":"1","message":"域名正常"}
            status为1是正常,为0是被封
        """
        if not domain:
            return {"status": "0", "message": "URL不能为空"}
        if not self._is_valid_domain(domain):
            return {"status": "0", "message": "域名格式不正确"}
        try:
            # 构建API请求URL
            api_url = f"https://api.wxapi.work/dy/api.Python?url={quote(domain)}"
            # 发送请求
            response = requests.get(api_url, headers=self.headers, timeout=self.timeout)
            response.raise_for_status()
            # 解析响应数据
            result = response.json()
            # 根据API响应处理结果
            if result.get('status') == 1:
                return {"status": "1", "message": "域名正常"}
            else:
                return {"status": "0", "message": "域名被封禁"}
        except requests.exceptions.RequestException as e:
            return {"status": "0", "message": f"网络请求失败: {str(e)}"}
        except json.JSONDecodeError:
            return {"status": "0", "message": "API响应数据解析失败"}
        except Exception as e:
            return {"status": "0", "message": f"检测异常: {str(e)}"}
    def check_wechat_miniprogram(self, appid: str) -> Dict[str, Any]:
        """
        检测微信小程序状态
        Args:
            appid: 微信小程序AppID
        Returns:
            dict: 检测结果 {"code":0,"appid":"wx81894c6dbb81c2e2","status":"已被封禁"}
            code为1是正常,为0是被封
        """
        if not appid or not self._is_valid_appid(appid):
            return {"code": "0", "appid": appid, "status": "AppID格式不正确"}
        try:
            # 构建API请求URL
            api_url = f"https://api.wxapi.work/xcx/checkxcx.Python?appid={quote(appid)}"
            # 发送请求
            response = requests.get(api_url, headers=self.headers, timeout=self.timeout)
            response.raise_for_status()
            # 解析响应数据
            result = response.json()
            # 处理小程序状态
            if result.get('code') == 0:
                return {
                    "code": "0",
                    "appid": result.get('appid', appid),
                    "status": result.get('status', '已被封禁')
                }
            else:
                return {
                    "code": "1",
                    "appid": result.get('appid', appid),
                    "status": "小程序正常"
                }
        except requests.exceptions.RequestException as e:
            return {"code": "0", "appid": appid, "status": f"网络请求失败: {str(e)}"}
        except json.JSONDecodeError:
            return {"code": "0", "appid": appid, "status": "API响应数据解析失败"}
        except Exception as e:
            return {"code": "0", "appid": appid, "status": f"检测异常: {str(e)}"}
    def _is_valid_domain(self, domain: str) -> bool:
        """
        验证域名格式
        Args:
            domain: 域名
        Returns:
            bool: 是否有效
        """
        import re
        pattern = r'^([a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,}$'
        return bool(re.match(pattern, domain))
    def _is_valid_appid(self, appid: str) -> bool:
        """
        验证微信小程序AppID格式
        Args:
            appid: 小程序AppID
        Returns:
            bool: 是否有效
        """
        import re
        pattern = r'^wx[0-9a-fA-F]{16}$'
        return bool(re.match(pattern, appid))
    def batch_check_domains(self, domains: list, check_type: str = 'qq') -> list:
        """
        批量检测域名
        Args:
            domains: 域名列表
            check_type: 检测类型 ('qq', 'wechat', 'douyin')
        Returns:
            list: 检测结果列表
        """
        results = []
        check_methods = {
            'qq': self.check_qq_domain,
            'wechat': self.check_wechat_domain,
            'douyin': self.check_douyin_domain
        }
        if check_type not in check_methods:
            return [{"error": f"不支持的检测类型: {check_type}"} for _ in domains]
        check_method = check_methods[check_type]
        for domain in domains:
            result = check_method(domain)
            result['domain'] = domain
            results.append(result)
        return results
def main():
    """
    主函数 - 演示使用方法
    """
    print("=" * 60)
    print("2025年10月最新Python域名检测工具")
    print("=" * 60)
    # 创建检测器实例
    checker = DomainChecker(timeout=10)
    # 示例1: 检测QQ域名
    print("\n1. QQ域名检测示例:")
    qq_result = checker.check_qq_domain("baidu.com")
    print(f"检测结果: {json.dumps(qq_result, ensure_ascii=False)}")
    # 示例2: 检测微信域名
    print("\n2. 微信域名检测示例:")
    wechat_result = checker.check_wechat_domain("baidu.com")
    print(f"检测结果: {json.dumps(wechat_result, ensure_ascii=False)}")
    # 示例3: 检测抖音域名
    print("\n3. 抖音域名检测示例:")
    douyin_result = checker.check_douyin_domain("baidu.com")
    print(f"检测结果: {json.dumps(douyin_result, ensure_ascii=False)}")
    # 示例4: 检测微信小程序
    print("\n4. 微信小程序检测示例:")
    mp_result = checker.check_wechat_miniprogram("wx81894c6dbb81c2e2")
    print(f"检测结果: {json.dumps(mp_result, ensure_ascii=False)}")
    # 示例5: 批量检测
    print("\n5. 批量QQ域名检测示例:")
    domains = ["baidu.com", "qq.com", "google.com"]
    batch_results = checker.batch_check_domains(domains, 'qq')
    for result in batch_results:
        print(f"{result['domain']}: {result['message']}")
if __name__ == "__main__":
    main()

使用方法

1. 环境要求

# 安装依赖
pip install requests

2. 基本使用

from domain_checker import DomainChecker
# 创建检测器
checker = DomainChecker(timeout=10)
# 检测QQ域名
result = checker.check_qq_domain("baidu.com")
print(result)  # {"status":"1","message":"域名正常"}
# 检测微信域名
result = checker.check_wechat_domain("baidu.com")
print(result)  # {"status":"2","message":"域名正常"}
# 检测抖音域名
result = checker.check_douyin_domain("baidu.com")
print(result)  # {"status":"1","message":"域名正常"}
# 检测微信小程序
result = checker.check_wechat_miniprogram("wx81894c6dbb81c2e2")
print(result)  # {"code":"1","appid":"wx81894c6dbb81c2e2","status":"小程序正常"}

3. 批量检测

# 批量检测多个域名
domains = ["example.com", "test.com", "demo.com"]
results = checker.batch_check_domains(domains, 'qq')
for result in results:
    print(f"域名: {result['domain']}, 状态: {result['message']}")

4. 错误处理

try:
    result = checker.check_qq_domain("invalid-domain")
    if result["status"] == "0":
        print(f"检测失败: {result['message']}")
except Exception as e:
    print(f"发生错误: {e}")

返回结果说明

QQ域名检测

{"status":"1","message":"域名正常"}
  • status为1是正常,为0是被封

微信域名检测

{"status":"2","message":"域名正常"}
  • status为1是被拦截,为0是被封,2是正常

抖音域名检测

{"status":"1","message":"域名正常"}
  • status为1是正常,为0是被封

微信小程序检测

{"code":"1","appid":"wx81894c6dbb81c2e2","status":"小程序正常"}
  • code为1是正常,为0是被封

功能特点

  1. ​2025年10月最新接口​​ - 使用最新的API端点

  2. ​完整的错误处理​​ - 完善的异常捕获机制

  3. ​参数验证​​ - 自动验证域名和AppID格式

  4. ​批量检测支持​​ - 支持同时检测多个域名

  5. ​类型注解​​ - 完整的类型提示

  6. ​详细文档​​ - 完整的注释和使用说明

注意事项

  1. 确保网络连接正常,能够访问检测API

  2. 合理设置超时时间,避免长时间等待

  3. 批量检测时注意频率限制

  4. 生产环境建议添加日志记录和缓存机制

这个Python工具类提供了完整的域名和小程序状态检测功能,代码简洁易用,适合集成到各种项目中。

posted on 2025-11-09 15:23  ljbguanli  阅读(0)  评论(0)    收藏  举报