我正在Ubuntu 16上使用python 3.5。
I am using python 3.5 on Ubuntu 16.
我正在尝试使用aiohttp编写一个简单的客户端。
I am trying to use aiohttp to write a simple client.
这是我的代码。我从此处中获取了它。这是第一个代码示例,禁用了ssl检查:
Here is the code I have. I took it from here. It's the first code sample, with ssl check disabled:
import aiohttp import asyncio import async_timeout async def fetch(session, url): with async_timeout.timeout(10): async with session.get(url) as response: return await response.text() async def main(loop): conn = aiohttp.TCPConnector(verify_ssl=False) async with aiohttp.ClientSession(loop=loop, connector=conn) as session: html = await fetch(session, 'www.google') print(html) loop = asyncio.get_event_loop() loop.run_until_complete(main(loop)) loop = asyncio.get_event_loop() loop.run_until_complete(main(loop))对于某些网站,此代码有效。对于其他用户,包括 python 或 google ,它不起作用。相反,代码会生成以下错误:
For some sites, this code works. For others, including python or google it does not work. Instead, the code generates this error:
aiohttp.errors.ClientOSError: [Errno 101] Cannot connect to host google:80 ssl:False [Can not connect to google:80 [Network is unreachable]]I尝试了一个简单的 requests 脚本,如下所示:
I tried a simple requests script, something like this:
import requests rsp = requests.get('google') print(rsp.text)这可行,我可以联系到Google。 curl和wget都可以到达google。
This works, I am able to reach google. Both curl and wget also reach google.
做一些研究后,我遇到了一个不同的问题。这个问题与我自己的问题相似。我在此处找到了它。我尝试了此处提供的解决方案,但仍然无法使用。
Doing some research, I came across a different problem. That problem is similar to my own. I found it here. I tried the solution offered here, but it still does not work.
并非所有站点都出现此问题。我遇到了正常工作和无效的http和https网站。
This issue does not occur for all sites. I came across both http and https sites that worked and did not work.
关于为什么会发生这种情况以及如何解决此问题的任何建议?
Any suggestions on why this happens and how can I fix this?
谢谢!
注意:
我尝试过的其他方法。
推荐答案
使用AsyncResolver作为连接的解析器时,我遇到了类似的问题。它曾经是默认的解析器,因此可能会因您的情况而有所不同。问题与ipv6的域有关,其中AsyncResolver有问题,因此解决方案是简单地指定族到ipv4地址
I had a similar issue when using AsyncResolver as the resolver for the connection. It used to be the default resolver so it might by your case. The problem was related to domains with ipv6 where the AsyncResolver has problems so the solution was to simply specify the family to ipv4 addresses
conn = aiohttp.TCPConnector( family=socket.AF_INET, verify_ssl=False, )更多推荐
python 3.5 asyncio和aiohttp Errno 101网络无法访问
发布评论