Python微信域名封禁状态检测工具

Python微信域名封禁状态检测工具_第1张图片

一、工具介绍

在微信生态中,域名被封禁或拦截会导致链接无法访问,影响业务正常运行。本文介绍的Python工具可通过调用接口(https://api.wxapi.work/wx/)检测域名在微信中的状态。接口返回数据中,status=2表示正常,status=1表示被拦截,status=0表示被封禁,帮助开发者快速定位域名访问问题,及时处理异常情况。

二、Python检测代码实现

import requests
import json
import sys

def check_domain_status(domain):
    """
    检测域名在微信中的封禁状态
    :param domain: 待检测的域名(如baidu.com)
    :return: 包含状态信息的字典
    """
    # 拼接接口URL,传入域名参数
    url = f"https://api.wxapi.work/wx/?url={domain}"
    
    try:
        # 发送GET请求,设置超时时间为10秒
        response = requests.get(url, timeout=10)
        
        # 检查响应状态码
        if response.status_code == 200:
            result = response.json()
            
            # 解析状态结果
            status = result.get("status")
            status_msg = result.get("status_message", "")
            
            # 格式化状态描述
            if status == 2:
                status_text = "域名正常"
            elif status == 1:
                status_text = "域名被拦截"
            elif status == 0:
                status_text = "域名被封禁"
            else:
                status_text = f"未知状态码: {status}"
            
            return {
                "success": True,
                "status_code": status,
                "status_text": status_text,
                "message": status_msg,
                "domain": domain
            }
        else:
            return {
                "success": False,
                "error": f"请求失败,状态码: {response.status_code}"
            }
    
    except requests.exceptions.RequestException as e:
        return {
            "success": False,
            "error": f"网络请求异常: {str(e)}"
        }
    except json.JSONDecodeError as e:
        return {
            "success": False,
            "error": f"JSON解析失败: {str(e)}"
        }
    except Exception as e:
        return {
            "success": False,
            "error": f"发生未知错误: {str(e)}"
        }

if __name__ == "__main__":
    # 获取命令行参数或用户输入的域名
    if len(sys.argv) > 1:
        target_domain = sys.argv[1]
    else:
        target_domain = input("请输入要检测的域名(如baidu.com): ").strip()
    
    if not target_domain:
        print("错误:域名不能为空")
        sys.exit(1)
    
    # 执行检测
    result = check_domain_status(target_domain)
    
    # 输出检测结果
    print("\n===== 微信域名状态检测结果 =====")
    if result["success"]:
        print(f"检测域名: {result['domain']}")
        print(f"状态码: {result['status_code']}")
        print(f"状态描述: {result['status_text']}")
        print(f"详细信息: {result['message']}")
    else:
        print(f"检测失败: {result['error']}")
    print("=================================\n")

三、使用方法

  1. 环境准备

    • 安装Python 3.6+版本
    • 安装requests库:在命令行执行pip install requests
  2. 代码使用步骤

    • 将代码保存为.py文件(如wechat_domain_checker.py
    • 方式一:命令行参数检测
      在命令行输入:python wechat_domain_checker.py baidu.com
      示例输出:

      ===== 微信域名状态检测结果 =====
      检测域名: baidu.com
      状态码: 2
      状态描述: 域名正常
      详细信息: 域名正常,添加微信互相交流学习:13092198273
      =================================
    • 方式二:交互式输入检测
      直接运行代码:python wechat_domain_checker.py,根据提示输入域名(如baidu.com),程序会返回检测结果。
  3. 结果解读

    • status=2:域名正常,可在微信中正常访问;
    • status=1:域名被拦截,用户访问时可能看到风险提示;
    • status=0:域名被封禁,无法在微信中打开。

四、拓展应用

  • 可将工具集成到网站监控系统中,定时检测业务域名状态;
  • 批量检测多个域名时,可修改代码添加循环逻辑,传入域名列表批量查询;
  • 结合自动化通知机制(如邮件、企业微信),在域名状态异常时实时告警。### Python实现微信域名封禁状态检测工具

一、工具介绍

在微信生态中,域名状态直接影响业务访问与用户体验。本文将介绍如何使用Python开发一个微信域名封禁状态检测工具,通过调用指定接口(https://api.wxapi.work/wx/)获取域名在微信内的状态信息。接口返回数据中,status=0表示域名被封,status=1表示域名被拦截,status=2表示域名正常。该工具可帮助开发者快速排查域名状态,及时处理异常情况。

二、Python检测代码实现

import requests
import json
import time

def check_wechat_domain_status(domain):
    """
    检测微信域名封禁状态
    :param domain: 待检测的域名
    :return: 包含状态信息的字典
    """
    # 接口URL,拼接域名参数
    url = f"https://api.wxapi.work/wx/?url={domain}"
    
    try:
        # 发送GET请求
        response = requests.get(url, timeout=10)
        
        # 解析JSON响应
        if response.status_code == 200:
            result = response.json()
            
            # 定义状态映射
            status_map = {
                "0": "已被封禁",
                "1": "被拦截",
                "2": "正常"
            }
            
            # 获取状态文本
            status_code = result.get("status", "")
            status_text = status_map.get(status_code, "未知状态")
            
            # 格式化输出结果
            print(f"【微信域名状态检测结果】")
            print(f"域名: {domain}")
            print(f"状态码: {status_code}")
            print(f"状态: {status_text}")
            
            if result.get("message"):
                print(f"信息: {result.get('message')}")
            
            return result
        else:
            print(f"请求失败,状态码: {response.status_code}")
            return {"error": f"请求失败,状态码: {response.status_code}"}
    
    except requests.exceptions.RequestException as e:
        print(f"请求异常: {str(e)}")
        return {"error": f"请求异常: {str(e)}"}
    except json.JSONDecodeError as e:
        print(f"解析响应失败: {str(e)}")
        return {"error": f"解析响应失败: {str(e)}"}

if __name__ == "__main__":
    # 示例:检测指定域名的状态
    example_domain = "baidu.com"
    print(f"正在检测域名: {example_domain} 在微信中的状态...")
    check_wechat_domain_status(example_domain)
    
    # 可取消下方注释,自定义输入域名
    # user_domain = input("请输入要检测的域名: ")
    # check_wechat_domain_status(user_domain)

三、使用方法

  1. 环境准备

    • 安装Python 3.6+版本
    • 安装requests库:在命令行执行pip install requests
  2. 代码使用步骤

    • 将上述代码保存为.py文件(如wechat_domain_checker.py
    • 方式一:直接运行代码,程序会默认检测示例域名(baidu.com),输出结果如下:

      正在检测域名: baidu.com 在微信中的状态...
      【微信域名状态检测结果】
      域名: baidu.com
      状态码: 2
      状态: 正常
      信息: 域名正常
    • 方式二:取消代码中user_domain部分的注释,运行后手动输入目标域名,程序将检测并显示对应状态。
  3. 结果解读

    • 若返回status=2,则域名在微信中状态正常,可正常访问;
    • 若返回status=1,则域名被微信拦截,需检查内容合规性;
    • 若返回status=0,则域名已被微信封禁,需联系微信官方处理。

四、拓展应用

  • 可将此工具集成到自动化监控系统中,定时检测域名状态并发送告警;
  • 支持批量检测多个域名,通过循环调用检测函数实现;
  • 结合域名解析服务,实现自动切换备用域名功能,保障业务连续性。

你可能感兴趣的:(python)