python 3.5 asyncio和aiohttp Errno 101网络无法访问 [英] python 3.5 asyncio and aiohttp Errno 101 Network is unreachable

查看:141
本文介绍了python 3.5 asyncio和aiohttp Errno 101网络无法访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在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, 'http://www.google.com')
        print(html)

loop = asyncio.get_event_loop()
loop.run_until_complete(main(loop))

loop = asyncio.get_event_loop()
loop.run_until_complete(main(loop))

对于某些网站,此代码有效。对于其他用户,包括 http://python.org http://google.com ,它不起作用。相反,代码会生成以下错误:

For some sites, this code works. For others, including http://python.org or http://google.com it does not work. Instead, the code generates this error:

aiohttp.errors.ClientOSError: [Errno 101] Cannot connect to host google.com:80 ssl:False [Can not connect to google.com:80 [Network is unreachable]]

I尝试了一个简单的 requests 脚本,如下所示:

I tried a simple requests script, something like this:

import requests
rsp = requests.get('http://google.com')
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?

谢谢!

注意:

我尝试过的其他方法。


  1. 也使用aiohttp添加我自己的DNS解析器。

  2. 使用网站的https版本,相同的错误。

  3. 使用略有不同的网址,例如 https://www.google.com/?#q=python

  1. Adding my own DNS resolver, also using aiohttp.
  2. Using the https version of the sites, getting the same error.
  3. Going to a slightly different url, for example https://www.google.com/?#q=python


推荐答案

使用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网络无法访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆