From 5cc138b69ab662ded7905b24a9066befbbe14506 Mon Sep 17 00:00:00 2001 From: huangzhenpc Date: Wed, 26 Feb 2025 10:21:48 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- batch_registration_example.py | 8 +++--- email_api_client.py | 9 ++++--- test_email_by_address.py | 46 +++++++++++++++++++++-------------- 3 files changed, 38 insertions(+), 25 deletions(-) diff --git a/batch_registration_example.py b/batch_registration_example.py index 1daac05..520a275 100644 --- a/batch_registration_example.py +++ b/batch_registration_example.py @@ -91,7 +91,7 @@ def register_account(client, domain, username=None): 'message': '未收到验证邮件' } -def batch_register(domain, count, concurrent=2): +def batch_register(domain, count, concurrent=2, api_url=None): """ 批量注册账号 @@ -99,6 +99,7 @@ def batch_register(domain, count, concurrent=2): domain: 邮箱域名 count: 注册数量 concurrent: 并发数 + api_url: API基础URL,默认为None使用客户端默认值 返回: 注册结果列表 @@ -106,7 +107,7 @@ def batch_register(domain, count, concurrent=2): logger.info(f"开始批量注册 {count} 个账号,域名: {domain},并发数: {concurrent}") # 创建API客户端 - client = EmailApiClient() + client = EmailApiClient(api_base_url=api_url) if api_url else EmailApiClient() # 检查系统状态 status = client.check_system_status() @@ -178,11 +179,12 @@ def main(): parser.add_argument('--count', type=int, default=5, help='注册数量') parser.add_argument('--concurrent', type=int, default=2, help='并发数') parser.add_argument('--output', type=str, default='registration_results.json', help='结果输出文件') + parser.add_argument('--api-url', type=str, help='邮件系统API基础URL,默认为http://74.48.75.19:5000/api') args = parser.parse_args() # 执行批量注册 - results = batch_register(args.domain, args.count, args.concurrent) + results = batch_register(args.domain, args.count, args.concurrent, args.api_url) # 保存结果 if results: diff --git a/email_api_client.py b/email_api_client.py index c30c562..4f49399 100644 --- a/email_api_client.py +++ b/email_api_client.py @@ -5,6 +5,7 @@ import requests import json import time import logging +import re class EmailApiClient: """ @@ -12,7 +13,7 @@ class EmailApiClient: 用于在批量注册场景中与邮件系统API交互 """ - def __init__(self, api_base_url="http://localhost:5000/api", timeout=10): + def __init__(self, api_base_url="http://74.48.75.19:5000/api", timeout=10): """ 初始化API客户端 @@ -22,15 +23,17 @@ class EmailApiClient: """ self.api_base_url = api_base_url self.timeout = timeout - self.logger = logging.getLogger("EmailApiClient") # 设置日志 + self.logger = logging.getLogger("EmailApiClient") if not self.logger.handlers: handler = logging.StreamHandler() formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') handler.setFormatter(formatter) self.logger.addHandler(handler) self.logger.setLevel(logging.INFO) + + self.logger.info(f"初始化API客户端: {api_base_url}") def get_emails_by_address(self, email_address, limit=10, unread_only=False, since=None): """ @@ -151,8 +154,6 @@ class EmailApiClient: if email.get('verification_code'): return email.get('verification_code') - import re - # 默认验证码模式 default_patterns = [ r'验证码[::\s]+(\d{4,8})', diff --git a/test_email_by_address.py b/test_email_by_address.py index 036d785..06a91eb 100644 --- a/test_email_by_address.py +++ b/test_email_by_address.py @@ -19,37 +19,47 @@ def get_emails_by_address(email_address, limit=10, unread_only=False, since=None 返回: API响应的JSON数据 """ - base_url = "http://localhost:5000/api/emails/by-address" + # 构建API URL + api_url = f"http://74.48.75.19:5000/api/emails/by-address" - # 构建参数 + # 构建查询参数 params = { - 'email_address': email_address, - 'limit': limit + "email_address": email_address, + "limit": limit } + # 添加可选参数 if unread_only: - params['unread_only'] = 'true' - + params["unread_only"] = "true" if since: - params['since'] = since + params["since"] = since + + # 打印请求信息 + print(f"请求URL: {api_url}") + print(f"参数: {params}") - # 发送请求 try: - print(f"请求 URL: {base_url}?{'&'.join([f'{k}={v}' for k, v in params.items()])}") - response = requests.get(base_url, params=params) + # 发送API请求 + response = requests.get(api_url, params=params, timeout=15) - # 打印响应信息 - print(f"状态码: {response.status_code}") + # 打印响应状态 + print(f"响应状态码: {response.status_code}") + # 返回JSON数据 if response.status_code == 200: - data = response.json() - return data + return response.json() else: - print(f"请求失败: {response.text}") - return None + print(f"API请求失败: {response.text}") + return { + "success": False, + "error": f"请求失败: HTTP {response.status_code}" + } except Exception as e: - print(f"请求出错: {str(e)}") - return None + print(f"发生错误: {str(e)}") + return { + "success": False, + "error": f"发生异常: {str(e)}" + } def format_email_info(email): """格式化邮件信息显示"""