python-3.x - 为什么我无法连接代理?
问题描述
我写了一个小脚本来检查代理:
async def proxy_check(session, proxy):
global good_proxies
proxy_str = f'http://{proxy}'
async with semaphore:
try:
async with session.get(host, proxy=proxy_str, timeout=10) as r:
if r.status == 200:
resp = await r.json()
if resp['ip'] == proxy:
good_proxies.append(proxy)
proxies.remove(proxy)
except Exception:
logging.exception(proxy)
proxies.remove(proxy)
async def main():
async with aiohttp.ClientSession() as session:
tasks = []
for proxy in proxies:
tasks.append(asyncio.create_task(proxy_check(session, proxy)))
await asyncio.gather(*tasks)
但是当我运行它时,我得到了以下错误之一:
aiohttp.http_exceptions.BadHttpMessage: 400, message='无效的常量字符串' aiohttp.client_exceptions.ClientResponseError: 400, message='无效的常量字符串' concurrent.futures._base.TimeoutError
我的列表中有近 20,000 个代理,并且此脚本无法通过所有这些代理进行连接。没有一个代理在此脚本中不起作用。
但如果你这样做:
proxy = {'http': f'http://{proxy}'}
r = requests.get(url, proxies=proxy)
一切正常。许多代理工作。我做错了什么?
解决方案
该集合proxies
在您的主要方法中进行迭代。它是由多个任务并行处理的元素。到目前为止这很好,但在处理功能中,您正在更改您正在处理的集合。这会导致竞争条件导致您正在迭代的集合损坏。
- 你永远不应该改变你正在处理的收藏。
- 如果您有代码并行更改共享资源,则需要使用互斥来使其线程安全。您可以在 python 3.7 中使用“锁定”。
推荐阅读
- php - ajax回调后增量计数器不起作用
- python - 在 pandas read_excel 中指定 index_col 时忽略 dtype 参数
- javascript - NodeJS 和 Mysql 连接配置被忽略
- python-3.x - 循环将熊猫数据帧的特定行中的字符串写入 PDF
- html - 如何在左右突出一个div
- python - 如果两个列表包含的表面 pygame 对象相等,我该如何检查?
- amazon-web-services - 如何从 terraform 创建 ansible 库存?
- c# - 我怎样才能让火箭围绕它的底部旋转,并统一到中间?
- python - 使用生成器为数据帧创建掩码函数
- ios - 如何保持 WidgetKit 上的现有条目刷新?