python 3.5 asyncio和aiohttp Errno 101网络无法访问

编程入门 行业动态 更新时间:2024-10-27 08:35:40
本文介绍了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, '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?

谢谢!

注意:

我尝试过的其他方法。

  • 也使用aiohttp添加我自己的DNS解析器。
  • 使用网站的https版本,相同的错误。
  • 使用略有不同的网址,例如 www.google/?#q=python
  • Adding my own DNS resolver, also using aiohttp.
  • Using the https version of the sites, getting the same error.
  • Going to a slightly different url, for example www.google/?#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网络无法访问

    本文发布于:2023-11-23 09:37:51,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1620966.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:无法访问   网络   asyncio   python   Errno

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!