Forums

Aiohttp client throws an error: ClientConnectorError: Cannot connect to host api.telegram.org:443 ssl:default [Network is unreachable]

Hello Everyone

I want to choose pythonanywhere for my chat telegram bot, but I have 1 error after start my python script. I spent a lot of time but didn't deal with that problem. It will be great if somebody helps me, because i want to upgrade my free account and use this platform for my bots, but it doesn't make sense because of that problem(


requirements: aiohttp-3.7.3 aiogram-2.10.1

from aiogram import Bot, Dispatcher, executor
from aiogram import types

BOT_TOKEN=""#Your telegram bot token
bot =Bot(BOT_TOKEN)
dp = Dispatcher(bot)
@dp.message_handler(text='test')
async def stasr1(message: types.Message):
    await message.answer(text='+')

executor.start_polling(dp)

Error: aiogram.utils.exceptions.NetworkError: Aiohttp client throws an error: ClientConnectorError: Cannot connect to host api.telegram.org:443 ssl:default [Network is unreachable]

enter image description here

enter image description here

Hi, we have a help page for such errors: https://help.pythonanywhere.com/pages/403ForbiddenError/ . Did you configure proxy?

I was trying to use proxy, but it works only on my local pc. Not in pythonanywhere

enter image description here

May be if i upgrade my pythonanywhere account everything will be ok?

Make sure you're using correct proxy details from the help page I posted earlier. If you upgrade your account it should solve your problem as well.

I bought hacker account and it decides my problem

enter image description here

.

:-)

help me

With what?

with the same problem but i wanna test it not buy if i like it then i will

If you want to test you can upgrade and if it does not work for you, in first 30 days you can downgrade and we will refund you.

Please, explain me how to cofigure proxy in my bot with library aiogram

All you need is add proxy to your bot like

PROXY_URL = "http://proxy.server:3128"

bot = Bot(proxy=PROXY_URL)

thank you

it worked

help me

faced the same problem as above did like here:

PROXY_URL = "http://proxy.server:3128"

bot = Bot(proxy=PROXY_URL)

it worked at first but now it gives an error:

aiogram.utils.exceptions.NetworkError: Aiohttp client throws an error: ClientConnectionError: Cannot initialize a TLS-in-TLS connection to host api.telegram.org:443 through an underlying connection to an HTTPS proxy http://proxy.server:3128 ssl:default [sslcontext is expected to be an instance of ssl.SSLContext, got <ssl.SSLContext object at 0x7fe8039bab40>]

The last bit of that error (the bit in square brackets) suggests that there may be an incompatibility between some of the libraries that you're using. Since the ssl module is a builtin, that suggests that you're using an incompatible version of Python with aiogram. Check the versions of Python that are supported by the version of aiogram that you're using and make sure you use a compatible one.

thank you!!!

Same problem with proxy is solved <3

Hello! I solved the problem with the proxy, but then the error "ProxyError('Cannot connect to proxy.', OSError('Tunnel connection failed: 403 Forbidden')))" came out when parsing. Am I right that you can't parse on a free account?

It looks like free account problem.

https://blog.pythonanywhere.com/148/ Update 2020-01-12 For some reason Telegram has stopped accepting wildcard HTTPS certificates for bots, so unfortunately this tutorial will no longer work on a PythonAnywhere free account. For the webhooks part of the tutorial to work, you will have to get a paying account with a custom domain. Thanks to fivechar for letting us know in the comments.

I don't think that that is true any more; wildcard certs appear to be working again.

.

13:06 ~ $ cd hack
13:06 ~/hack $ python bot.py
WARNING:aiogram:Goodbye!
Traceback (most recent call last):
  File "/home/Osaka765/.local/lib/python3.10/site-packages/aiohttp/connector.py", line 980, in _wrap_create_connection
    return await self._loop.create_connection(*args, **kwargs)  # type: ignore[return-value]  # noqa
  File "/usr/local/lib/python3.10/asyncio/base_events.py", line 1064, in create_connection
    raise exceptions[0]
  File "/usr/local/lib/python3.10/asyncio/base_events.py", line 1049, in create_connection
    sock = await self._connect_sock(
  File "/usr/local/lib/python3.10/asyncio/base_events.py", line 960, in _connect_sock
    await self.sock_connect(sock, address)
  File "/usr/local/lib/python3.10/asyncio/selector_events.py", line 500, in sock_connect
    return await fut
  File "/usr/local/lib/python3.10/asyncio/selector_events.py", line 505, in _sock_connect
    sock.connect(address)
OSError: [Errno 101] Network is unreachable
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
  File "/home/Osaka765/.local/lib/python3.10/site-packages/aiogram/bot/api.py", line 139, in make_request
    async with session.post(url, data=req, **kwargs) as response:
  File "/home/Osaka765/.local/lib/python3.10/site-packages/aiohttp/client.py", line 1141, in __aenter__
    self._resp = await self._coro
  File "/home/Osaka765/.local/lib/python3.10/site-packages/aiohttp/client.py", line 536, in _request
    conn = await self._connector.connect(
  File "/home/Osaka765/.local/lib/python3.10/site-packages/aiohttp/connector.py", line 540, in connect
    proto = await self._create_connection(req, traces, timeout)
  File "/home/Osaka765/.local/lib/python3.10/site-packages/aiohttp/connector.py", line 901, in _create_connection
    _, proto = await self._create_direct_connection(req, traces, timeout)
  File "/home/Osaka765/.local/lib/python3.10/site-packages/aiohttp/connector.py", line 1206, in _create_direct_connection
    raise last_exc
  File "/home/Osaka765/.local/lib/python3.10/site-packages/aiohttp/connector.py", line 1175, in _create_direct_connection
    transp, proto = await self._wrap_create_connection(
  File "/home/Osaka765/.local/lib/python3.10/site-packages/aiohttp/connector.py", line 988, in _wrap_create_connection
    raise client_error(req.connection_key, exc) from exc
aiohttp.client_exceptions.ClientConnectorError: Cannot connect to host api.telegram.org:443 ssl:default [Network is unreachable]
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "/home/Osaka765/hack/bot.py", line 96, in <module>
    executor.start_polling(dp, skip_updates=True)
  File "/home/Osaka765/.local/lib/python3.10/site-packages/aiogram/utils/executor.py", line 45, in start_polling
    executor.start_polling(
  File "/home/Osaka765/.local/lib/python3.10/site-packages/aiogram/utils/executor.py", line 320, in start_polling
    loop.run_until_complete(self._startup_polling())
  File "/usr/local/lib/python3.10/asyncio/base_events.py", line 646, in run_until_complete
    return future.result()
  File "/home/Osaka765/.local/lib/python3.10/site-packages/aiogram/utils/executor.py", line 372, in _startup_polling
    await self._welcome()
  File "/home/Osaka765/.local/lib/python3.10/site-packages/aiogram/utils/executor.py", line 361, in _welcome
    user = await self.dispatcher.bot.me
  File "/home/Osaka765/.local/lib/python3.10/site-packages/aiogram/bot/bot.py", line 30, in me
    setattr(self, '_me', await self.get_me())
  File "/home/Osaka765/.local/lib/python3.10/site-packages/aiogram/bot/bot.py", line 233, in get_me
    result = await self.request(api.Methods.GET_ME, payload)
  File "/home/Osaka765/.local/lib/python3.10/site-packages/aiogram/bot/base.py", line 236, in request
    return await api.make_request(await self.get_session(), self.server, self.__token, method, data, files,
  File "/home/Osaka765/.local/lib/python3.10/site-packages/aiogram/bot/api.py", line 142, in make_request
    raise exceptions.NetworkError(f"aiohttp client throws an error: {e.__class__.__name__}: {e}")
aiogram.utils.exceptions.NetworkError: Aiohttp client throws an error: ClientConnectorError: Cannot connect to host api.telegram.org:443 ssl:default [Network is unreachable]
13:07 ~/hack $

Я немогу понять что оно хочет, помогите пожалуйста решить даную проблему... Буду благодарен! Спасибо!

[edit by admin: formatting]

It looks like the library you are using to connect to Telegram is not proxy-aware; you will need to find out how to configure it to use a proxy if you want to make outbound connections from a free account. You can find the hostname and the port for the proxy by running echo $http_proxy in Bash.

Hi team, I test with bing.com, which is on whitelist. but It's still give 404 not found. What could be a problem ?

from flask import Flask
from werkzeug.middleware.proxy_fix import ProxyFix

def create_app():
    app = Flask(__name__)
    # Add your Flask app configurations here
    return app

app = create_app()

def application(environ, start_response):
    # Forward the request to bing.com
    environ['HTTP_HOST'] = 'www.bing.com'
    environ['HTTP_X_FORWARDED_HOST'] = 'www.bing.com'
    environ['HTTP_PROXY'] = 'http://proxy.server:3128'
    return app(environ, start_response)

app = app.wsgi_app  # Use the app's WSGI application instead

if __name__ == '__main__':
    app.run()

Thanks

You have no routing set up in your app.

Does this proxy.server:3128 proxy still work?

Yes

how to add proxy on aiogram3?

Take a look at aiogram3 docs.

not working any more

What is not working for you?

У меня не работает, ошибка та же, что и в заголовке. Но прокси не помогает

What code have you used to configure the proxy?