python - aiohttp客户端连接错误,但是我可以在网上找到网站就好了?
问题描述
我正在尝试使用我在网上找到的此代码从 url 列表中异步返回 HTTP 请求代码,但是在打印了几次后,我收到错误 ClientConnectorError: Cannot connect to host website_i_removed :80 ssl:None [getaddrinfo failed]。由于该网站是有效的,我对它如何说我无法连接感到困惑。如果我在任何时候做错了,请指出我正确的方向。
在过去的几个小时里,我一直在查看 aiohttp 的文档和在线信息,但是他们没有关于带有 url 列表的 HTTP 请求的示例,而且他们在文档中的入门页面很难理解,因为我是新手异步编程。下面是我正在使用的代码,假设 urls 是一个字符串列表。
import asyncio
from aiohttp import ClientSession
async def fetch(url, session):
async with session.get(url) as response:
code_status = response.history[0].status if response.history else response.status
print('%s -> Status Code: %s' % (url, code_status))
return await response.read()
async def bound_fetch(semaphore, url, session):
# Getter function with semaphore.
async with semaphore:
await fetch(url, session)
async def run(urls):
tasks = []
# create instance of Semaphore
semaphore = asyncio.Semaphore(1000)
async with ClientSession() as session:
for url in urls:
# pass Semaphore and session to every GET request
task = asyncio.ensure_future(bound_fetch(semaphore, url, session))
tasks.append(task)
responses = asyncio.gather(*tasks)
await responses
loop = asyncio.get_event_loop()
future = asyncio.ensure_future(run(urls))
loop.run_until_complete(future)
我希望每个网站都打印其请求代码以确定它们是否可以访问,但是它说我无法连接到某些网站,尽管我可以在浏览器中查找它们。
解决方案
推荐阅读
- matlab - 在Matlab大纲条中使用不同颜色的条非常宽
- amazon-web-services - aws 自动缩放组,缩小条件
- python - 来自两个数据框的两行的产品总和并将总和添加到新列
- google-chrome - 当 index.html 在第二次请求时获得 304 时缓存静态资产?
- firebase - 没有为“UserCredential”类型定义方法“sendEmailVerification”。Firebase 颤动
- c - 为什么在 Mac 和 Ubuntu 中使用 strtok_r 的结果不同
- javascript - 最后一个“SetItem”不保存,页面刷新时清除
- html - 使 selenium python 不回复 python 中的相同消息
- jekyll - 尽可能快地通过路径/名称获取集合页面变量
- flutter - 调试打印使响应正文的输出不完整