Forums

ConnectionRefusedError: [Errno 111] Connect call failed ('149.154.167.51', 443)

Attempt 1 at connecting failed: ConnectionRefusedError: [Errno 111] Connect call failed ('149.154.167.51', 443) Attempt 2 at connecting failed: ConnectionRefusedError: [Errno 111] Connect call failed ('149.154.167.51', 443) Attempt 3 at connecting failed: ConnectionRefusedError: [Errno 111] Connect call failed ('149.154.167.51', 443) Attempt 4 at connecting failed: ConnectionRefusedError: [Errno 111] Connect call failed ('149.154.167.51', 443) Attempt 5 at connecting failed: ConnectionRefusedError: [Errno 111] Connect call failed ('149.154.167.51', 443) Attempt 6 at connecting failed: ConnectionRefusedError: [Errno 111] Connect call failed ('149.154.167.51', 443) Traceback (most recent call last): File "/home/Rajpatel29/main.py", line 23, in <module> client.start() File "/home/Rajpatel29/.local/lib/python3.10/site-packages/telethon/client/auth.py", line 128, in start else self.loop.run_until_complete(coro) File "/usr/local/lib/python3.10/asyncio/base_events.py", line 646, in run_until_complete return future.result() File "/home/Rajpatel29/.local/lib/python3.10/site-packages/telethon/client/auth.py", line 135, in _start await self.connect() File "/home/Rajpatel29/.local/lib/python3.10/site-packages/telethon/client/telegrambaseclient.py", line 538, in connect if not await self._sender.connect(self._connection( File "/home/Rajpatel29/.local/lib/python3.10/site-packages/telethon/network/mtprotosender.py", line 132, in connect await self._connect() File "/home/Rajpatel29/.local/lib/python3.10/site-packages/telethon/network/mtprotosender.py", line 258, in _connect raise ConnectionError('Connection to Telegram failed {} time(s)'.format(self._retries)) ConnectionError: Connection to Telegram failed 5 time(s)

Can you provide some more information and share your code?

The same problem. They write on the Internet that the problem with the connection is on the hosting side. The connection is on an unauthorized port. It can only be so with a telegram. How can this problem be solved? Because it also works on local machines and on the phone, but not on your hosting.

2 files are being created:

 data_main.session, 
 data_main.session-journal

Which are highlighted in black and are not clickable. That is, they do not have formats. Session data is stored there.

My mistake:

Attempt 1 at connecting failed: ConnectionRefusedError: [Errno 111] Connect call failed ('149.154.167.51', 443)
Attempt 2 at connecting failed: ConnectionRefusedError: [Errno 111] Connect call failed ('149.154.167.51', 443)
Attempt 3 at connecting failed: ConnectionRefusedError: [Errno 111] Connect call failed ('149.154.167.51', 443)
Attempt 4 at connecting failed: ConnectionRefusedError: [Errno 111] Connect call failed ('149.154.167.51', 443)
Attempt 5 at connecting failed: ConnectionRefusedError: [Errno 111] Connect call failed ('149.154.167.51', 443)
Attempt 6 at connecting failed: ConnectionRefusedError: [Errno 111] Connect call failed ('149.154.167.51', 443)
Traceback (most recent call last):
  File "/home/AlexSem529/spy.py", line 18, in <module>
    client.connect()
  File "/home/AlexSem529/.local/lib/python3.10/site-packages/telethon/sync.py", line 39, in syncified
    return loop.run_until_complete(coro)
  File "/usr/local/lib/python3.10/asyncio/base_events.py", line 646, in run_until_complete
    return future.result()
  File "/home/AlexSem529/.local/lib/python3.10/site-packages/telethon/client/telegrambaseclient.py", line 544, in connect
    if not await self._sender.connect(self._connection(
  File "/home/AlexSem529/.local/lib/python3.10/site-packages/telethon/network/mtprotosender.py", line 132, in connect
    await self._connect()
  File "/home/AlexSem529/.local/lib/python3.10/site-packages/telethon/network/mtprotosender.py", line 258, in _connect
    raise ConnectionError('Connection to Telegram failed {} time(s)'.format(self._retries))
ConnectionError: Connection to Telegram failed 5 time(s)

[edit by admin: formatting]

It looks to me like the library you are using is using Telegram's proprietary MTProto protocol to connect to their API. Free accounts on PythonAnywhere can only make outbound HTTP and HTTPS connections, and are restricted to accessing hosts on our allow-list. Although Telegram is on that list, MTProto is not based on HTTP or HTTPS so it will not work. You'll need to use a library that uses their HTTPS-based API.

That is, even if we bought a subscription to pythonanywhere, we still won't have access to the telegram api? Can you recommend a library for working with the telegram api via HTTPS? Will it fit Link for example

I think this package provides a Python interface to Telegram BOT API, and I saw recently some examples of a working code on a free account using that package. On a paid account you shouldn't have any internet restrictions to connect to external services, unless, for some reason, such service would block our servers.