commit
This commit is contained in:
126
backend/test_proxy_detailed.py
Normal file
126
backend/test_proxy_detailed.py
Normal file
@@ -0,0 +1,126 @@
|
||||
"""
|
||||
固定代理IP详细测试脚本
|
||||
测试代理IP在Playwright中的表现,包含更多调试信息
|
||||
"""
|
||||
import asyncio
|
||||
import json
|
||||
import sys
|
||||
from xhs_login import XHSLoginService
|
||||
from damai_proxy_config import get_proxy_config
|
||||
|
||||
|
||||
async def test_proxy_detailed(proxy_index: int = 0):
|
||||
"""详细测试代理IP"""
|
||||
print(f"\n{'='*60}")
|
||||
print(f"🔍 详细测试代理: 代理{proxy_index + 1}")
|
||||
print(f"{'='*60}")
|
||||
|
||||
# 获取代理配置
|
||||
try:
|
||||
proxy_config = get_proxy_config(proxy_index)
|
||||
proxy_url = f"http://{proxy_config['username']}:{proxy_config['password']}@{proxy_config['server'][7:]}" # 移除http://前缀再重新组装
|
||||
print(f"✅ 获取代理配置成功: 代理{proxy_index + 1}")
|
||||
print(f" 代理服务器: {proxy_config['server']}")
|
||||
except Exception as e:
|
||||
print(f"❌ 获取代理配置失败: {str(e)}")
|
||||
return None
|
||||
|
||||
# 创建登录服务实例
|
||||
login_service = XHSLoginService(use_pool=False) # 不使用池,便于调试
|
||||
|
||||
try:
|
||||
# 初始化浏览器(使用代理)
|
||||
print(f"\n🚀 正在启动浏览器(使用代理)...")
|
||||
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'
|
||||
await login_service.init_browser(proxy=proxy_url, user_agent=user_agent)
|
||||
print("✅ 浏览器启动成功")
|
||||
|
||||
# 测试访问普通网站
|
||||
print(f"\n📍 测试访问普通网站(百度)...")
|
||||
try:
|
||||
await login_service.page.goto('https://www.baidu.com', wait_until='networkidle', timeout=10000)
|
||||
await asyncio.sleep(2)
|
||||
title = await login_service.page.title()
|
||||
url = login_service.page.url
|
||||
print(f"✅ 百度访问成功")
|
||||
print(f" 页面标题: {title}")
|
||||
print(f" 当前URL: {url}")
|
||||
except Exception as e:
|
||||
print(f"❌ 百度访问失败: {str(e)}")
|
||||
|
||||
# 测试访问IP检测网站
|
||||
print(f"\n📍 测试访问IP检测网站...")
|
||||
try:
|
||||
await login_service.page.goto('http://httpbin.org/ip', wait_until='networkidle', timeout=10000)
|
||||
await asyncio.sleep(2)
|
||||
content = await login_service.page.content()
|
||||
print(f"✅ IP检测网站访问成功")
|
||||
print(f" 页面内容: {content[:200]}...")
|
||||
except Exception as e:
|
||||
print(f"❌ IP检测网站访问失败: {str(e)}")
|
||||
|
||||
# 测试访问小红书创作者平台
|
||||
print(f"\n📍 测试访问小红书创作者平台...")
|
||||
try:
|
||||
await login_service.page.goto('https://creator.xiaohongshu.com/login', wait_until='networkidle', timeout=20000) # 增加超时时间
|
||||
await asyncio.sleep(3) # 等待更长时间
|
||||
|
||||
title = await login_service.page.title()
|
||||
url = login_service.page.url
|
||||
print(f"✅ 小红书访问成功")
|
||||
print(f" 页面标题: '{title}'")
|
||||
print(f" 当前URL: {url}")
|
||||
|
||||
# 检查页面内容
|
||||
content = await login_service.page.content()
|
||||
if "验证" in content or "captcha" in content.lower() or "block" in content.lower() or "安全验证" in content:
|
||||
print("⚠️ 检测到可能的验证或拦截")
|
||||
else:
|
||||
print("✅ 未检测到验证拦截")
|
||||
|
||||
except Exception as e:
|
||||
print(f"❌ 小红书访问失败: {str(e)}")
|
||||
# 尝试访问普通页面看看是否完全被封
|
||||
try:
|
||||
await login_service.page.goto('https://www.google.com', wait_until='networkidle', timeout=10000)
|
||||
print(" 提示: 代理可以访问其他网站,但可能被小红书限制")
|
||||
except Exception:
|
||||
print(" 提示: 代理可能完全被限制")
|
||||
|
||||
print(f"\n✅ 代理{proxy_index + 1} 详细测试完成")
|
||||
return login_service
|
||||
|
||||
except Exception as e:
|
||||
print(f"❌ 代理{proxy_index + 1} 详细测试失败: {str(e)}")
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
return None
|
||||
finally:
|
||||
# 关闭浏览器
|
||||
await login_service.close_browser()
|
||||
|
||||
|
||||
async def main():
|
||||
"""主测试函数"""
|
||||
print("\n" + "="*60)
|
||||
print("🎯 固定代理IP详细测试")
|
||||
print("="*60)
|
||||
|
||||
# 测试两个代理
|
||||
for i in range(2):
|
||||
await test_proxy_detailed(i)
|
||||
print(f"\n⏳ 等待3秒后测试下一个代理...")
|
||||
await asyncio.sleep(3)
|
||||
|
||||
print(f"\n{'='*60}")
|
||||
print("🎉 详细测试完成!")
|
||||
print("="*60)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
# Windows环境下设置事件循环策略
|
||||
if sys.platform == 'win32':
|
||||
asyncio.set_event_loop_policy(asyncio.WindowsProactorEventLoopPolicy())
|
||||
|
||||
# 运行测试
|
||||
asyncio.run(main())
|
||||
Reference in New Issue
Block a user