here is my code:
import asyncio
from aiohttp import ClientSession
async def main():
url = "https://stackoverflow.com/"
async with ClientSession() as session:
async with session.get(url) as resp:
print(resp.status)
asyncio.run(main())
if I run it on my computer, everything works, but if I run it on pythonanywhere, I get this error:
Traceback (most recent call last):
File "/home/0dminnimda/.local/lib/python3.8/site-packages/aiohttp/connector.py", line 936, in _wrap_create_connection
return await self._loop.create_connection(*args, **kwargs) # type: ignore # noqa
File "/usr/lib/python3.8/asyncio/base_events.py", line 1017, in create_connection
raise exceptions[0]
File "/usr/lib/python3.8/asyncio/base_events.py", line 1002, in create_connection
sock = await self._connect_sock(
File "/usr/lib/python3.8/asyncio/base_events.py", line 916, in _connect_sock
await self.sock_connect(sock, address)
File "/usr/lib/python3.8/asyncio/selector_events.py", line 485, in sock_connect
return await fut
File "/usr/lib/python3.8/asyncio/selector_events.py", line 517, in _sock_connect_cb
raise OSError(err, f'Connect call failed {address}')
ConnectionRefusedError: [Errno 111] Connect call failed ('151.101.193.69', 443)
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "test_c.py", line 39, in <module>
asyncio.run(main())
File "/usr/lib/python3.8/asyncio/runners.py", line 43, in run
return loop.run_until_complete(main)
File "/usr/lib/python3.8/asyncio/base_events.py", line 608, in run_until_complete
return future.result()
File "test_c.py", line 28, in main
async with session.get(url, timeout=30) as resp: # , headers=headers
File "/home/0dminnimda/.local/lib/python3.8/site-packages/aiohttp/client.py", line 1012, in __aenter__
self._resp = await self._coro
File "/home/0dminnimda/.local/lib/python3.8/site-packages/aiohttp/client.py", line 480, in _request
conn = await self._connector.connect(
File "/home/0dminnimda/.local/lib/python3.8/site-packages/aiohttp/connector.py", line 523, in connect
proto = await self._create_connection(req, traces, timeout)
File "/home/0dminnimda/.local/lib/python3.8/site-packages/aiohttp/connector.py", line 858, in _create_connection
_, proto = await self._create_direct_connection(
File "/home/0dminnimda/.local/lib/python3.8/site-packages/aiohttp/connector.py", line 1004, in _create_direct_connection
raise last_exc
File "/home/0dminnimda/.local/lib/python3.8/site-packages/aiohttp/connector.py", line 980, in _create_direct_connection
transp, proto = await self._wrap_create_connection(
File "/home/0dminnimda/.local/lib/python3.8/site-packages/aiohttp/connector.py", line 943, in _wrap_create_connection
raise client_error(req.connection_key, exc) from exc
aiohttp.client_exceptions.ClientConnectorError: Cannot connect to host stackoverflow.com:443 ssl:default [Connect call failed ('151.101.193.69', 443)]
Unclosed client session
client_session: <aiohttp.client.ClientSession object at 0x7f25a71d1a90>
aiohttp on hosting:
Name: aiohttp
Version: 3.6.2
Summary: Async http client/server framework (asyncio)
Home-page: https://github.com/aio-libs/aiohttp
Author: Nikolay Kim
Author-email: fafhrd91@gmail.com
License: Apache 2
Location: /home/0dminnimda/.local/lib/python3.8/site-packages
Requires: chardet, async-timeout, multidict, yarl, attrs
Required-by:
aiohttp on my PC:
Name: aiohttp
Version: 3.6.2
Summary: Async http client/server framework (asyncio)
Home-page: https://github.com/aio-libs/aiohttp
Author: Nikolay Kim
Author-email: fafhrd91@gmail.com
License: Apache 2
Location: c:usersasusappdataroamingpythonpython38site-packages
Requires: async-timeout, attrs, chardet, yarl, multidict
Required-by:
I am at a loss that it is not so? I am running both files using python3.8.
I also tried other urls, they have the same problem
Do I need to add any more details?
OK, so, I have no idea if this is a general problem, but let me tell you how I fixed this for my system, which may just be about my system but who knows?, maybe it’ll help you.
I have hass.io running under docker on my x86_64 Linux box, so if anything I’m saying seems wildly inapplicable, that’s why. All the commands I give are run on the Docker host itself, unless otherwise specified.
The short version: This error means that your supervisor instance can’t reach port 8123 on your homeassistant instance. docker uses iptables to set up the networking between instances, so inspect the iptables rules very carefully, and see if something wacky is going on.
The specific commands that solved my problem, but are unlikely to solve yours, are:
sudo iptables -I INPUT -s 172.16.0.0/12 -j ACCEPT
sudo iptables -I INPUT -d 172.16.0.0/12 -j ACCEPT
(If those commands actually do solve this problem for a bunch of people, then something super wacky is going on with the supervisor’s docker setup and someone should open a new bug saying that these commands solve this problem, but that seems super unlikely.)
More details:
On my system, at least, I can test the issue directly by using «docker ps -a» to find the supervisor instance, then «docker exec -it [id] bash» and running «curl http://172.30.32.1:8123/», where 172.30.32.1 seems to be the normal IP for the homeassistant instance. You can also do «docker exec -it [homassistant-instance-id] bash» and run «ip addr» to confirm which IP is which.
If that curl hangs or is rejected, but the same curl works from the homeassistant instance, or works from the docker instance itself, then the issue is probably that the docker networking between the two instances is broken.
Here’s the details of the specific issue that was blocking docker networking in my case, but «docker networking is broken» is a HUGE topic; there are literally tens of thousands of threads on such issues all over the web.
For myself, I have iptables locked down pretty tight; I have default deny turned on for the INPUT chain in iptables. That is, the last rule in the INPUT chain is:
-A INPUT -j REJECT —reject-with icmp-host-prohibited
, and above that I have a bunch of white-listed stuff like port 22 or whatever.
Maintaining such a whitelist for all of my Docker stuff is a huge pain in the ass and is also kind of pointless, since I don’t really care about communications between two docker instances as long as nothing’s talking to the internet, so I have a special Docker whitelist like so:
-A INPUT -s 172.17.0.0/16 -j ACCEPT -A INPUT -d 172.17.0.0/16 -j ACCEPT
This is the normal range of Docker instance IPs I’ve seen in the past.
It turns out, however, that for whatever reason, the instances the hass.io supervisor generates aren’t in that range; they’re in 172.30.0.0/16
The fix was to just grab the entire 172.16 private range, which I should have done in the first place.
In other words, these commands solved the problem for me; I would be surprised if they solved the problem for you, but who knows?:
sudo iptables -I INPUT -s 172.16.0.0/12 -j ACCEPT
sudo iptables -I INPUT -d 172.16.0.0/12 -j ACCEPT
I have a situation where I am being forced to use nat on the vmware network. All machines, VirtualCenter, ESX servers, and host are behind the firewall that NAT’s us from the rest of the world. It is a one to one NAT and I can use my Virtual Infrastructure Client to access the VirtualCenter server from outside the private network. I cannot, however, access any of the consoles of the client machines on the esx server host.
I’ve checked DNS and can resolve both the host machine and client WITHIN the nat’ed network. I changed the host to use the IP address on VirtualCenter and the error message changes to error connecting: cannot connect to host 10.0.0.x: A connection attempt failed because the connected party did not properly respond after a period….
from
Error connecting: Host address lookup for server esx.x.x.com failed: The requested name is valid and was found in the database, but it does not have the correct associated data being resolved for (blank). Do you want to try again?
Now, I [/i] think[i][/i] this has to do with being nat’ed across the firewall. Any suggestions?
Loading
Get the Reddit app
Scan this QR code to download the app now
Or check it out in the app stores