使用 Python 测试动态住宅代理并发性能
发布时间2025-06-26 04:07:19在对动态住宅代理进行评估时,了解其并发承载能力是非常关键的一步。本文将介绍如何使用 Python 编写异步脚本,通过高并发请求测试代理的稳定性和可用性。
在对动态住宅代理进行评估时,了解其并发承载能力是非常关键的一步。本文将介绍如何使用 Python 编写异步脚本,通过高并发请求测试代理的稳定性和可用性。
一、前提依赖与安装
本脚本依赖以下第三方库:
aiohttp
: 异步 HTTP 请求库aiohttp_socks
: 支持 SOCKS5 代理的 aiohttp 扩展
安装依赖
pip install aiohttp aiohttp_socks
如果你使用的是 Python >= 3.11 且遇到安装 aiohttp
相关的编译问题,请尝试:
pip install --upgrade pip setuptools wheel
pip install aiohttp --prefer-binary
或者使用 conda
环境来规避编译错误。
⚙️ 二、脚本逻辑解析
以下是测试脚本的核心逻辑结构和说明。
1. 参数配置区域
proxy = "socks5://dfjkMMekh:uJOiAiGY@127.0.0.1:8080"
target_url = "https://app.m.kuaishou.com"
start_concurrency = 600
step = 200
max_concurrency = 5000
timeout_seconds = 8
failure_threshold = 0.2
proxy
: 动态住宅代理地址,支持账号密码验证。target_url
: 请求的目标网站,建议选取稳定、响应快的站点。start_concurrency
: 初始并发请求数量。step
: 每轮测试递增的并发数。max_concurrency
: 最大测试上限。timeout_seconds
: 单次请求超时时间。failure_threshold
: 失败率超过该值即中止测试。
2. 单次请求函数:fetch
async def fetch(session, index):
try:
async with session.get(target_url, timeout=timeout_seconds) as resp:
return resp.status == 200
except:
return False
每次通过代理发送异步 GET 请求,记录成功与失败(以 HTTP 200 为准)。
3. 并发批处理函数:run_batch
async def run_batch(concurrency):
connector = ProxyConnector.from_url(proxy)
async with aiohttp.ClientSession(connector=connector) as session:
tasks = [fetch(session, i) for i in range(concurrency)]
results = await asyncio.gather(*tasks, return_exceptions=True)
- 创建一个使用代理的连接器。
- 生成多个任务并同时执行。
- 使用
asyncio.gather(..., return_exceptions=True)
,防止部分任务抛异常时中断整体执行。
4. 主控逻辑:main
async def main():
concurrency = start_concurrency
while concurrency <= max_concurrency:
...
if fail_rate > failure_threshold:
...
break
concurrency += step
每轮以递增的并发数执行测试,输出成功/失败统计。当失败率超过阈值,测试终止,并报告最大可用并发。
5. 完整代码
import asyncio
from aiohttp_socks import ProxyConnector
import aiohttp
import time
# ====== 配置区域 ======
proxy = "socks5://dfjkMMekh:uJOiAiGY@127.0.0.1:8080"
target_url = "https://app.m.kuaishou.com"
start_concurrency = 600
step = 200
max_concurrency = 5000
timeout_seconds = 8
failure_threshold = 0.2
# =======================
async def fetch(session, index):
try:
async with session.get(target_url, timeout=timeout_seconds) as resp:
return resp.status == 200
except:
return False
async def run_batch(concurrency):
connector = ProxyConnector.from_url(proxy)
async with aiohttp.ClientSession(connector=connector) as session:
tasks = [fetch(session, i) for i in range(concurrency)]
results = await asyncio.gather(*tasks, return_exceptions=True)
success = sum(1 for r in results if r is True)
failure = concurrency - success
return success, failure
async def main():
concurrency = start_concurrency
while concurrency <= max_concurrency:
print(f"\n 正在测试并发: {concurrency} 请求 --> {target_url}")
start = time.time()
success, failure = await run_batch(concurrency)
duration = time.time() - start
fail_rate = failure / concurrency
success_rate = success / concurrency * 100
print(f" 成功: {success}")
print(f" 失败: {failure}")
print(f" 成功率: {success_rate:.2f}%")
print(f" 耗时: {duration:.2f} 秒")
if fail_rate > failure_threshold:
print(f"\n 失败率超过 {failure_threshold * 100:.0f}%,测试终止。")
print(f" 估计最大并发为: {concurrency - step}")
break
concurrency += step
if __name__ == "__main__":
asyncio.run(main())
上述就是本次测试的完整代码如果请求失败率超过或者接近50%,则证明当前代理的并发率超出了限制
三、常见报错与解决方案
报错信息 | 可能原因 | 解决建议 |
---|---|---|
ClientConnectorError |
代理地址错误、端口未开放、认证失败 | 检查代理地址、用户名密码、代理是否可用 |
TimeoutError |
请求超时 | 增加 timeout_seconds ,或减小并发数 |
Too many open files |
并发连接过多 | 调整系统 ulimit 限制,例如:ulimit -n 65535 |
ssl.SSLError: WRONG_VERSION_NUMBER |
HTTP 请求了 HTTPS,或代理协议不一致 | 确保代理协议为 SOCKS5,目标 URL 正确 |
四、运行输出示例
正在测试并发: 600 请求 --> https://app.m.kuaishou.com
成功: 598
失败: 2
成功率: 99.67%
耗时: 4.32 秒
...
失败率超过 20%,测试终止。
估计最大并发为: 2600
五、建议与注意事项
- 代理服务通常对高频并发有限制,建议控制测试频率避免 IP 被封。
- 测试目标建议为可访问的高可用服务。
- 多轮测试建议间隔 5-10 秒以上,避免网络阻塞。
六、总结
本方法结合了异步编程和代理连接,通过逐步增加并发量评估动态住宅代理的性能瓶颈,非常适用于代理质量评估与上限测试。
如需进一步优化脚本,可增加重试机制、记录失败原因、导出日志数据等。