Telethon - 服务器关闭连接错误

Telethon - The server closed the connection error

我无法使用 Telethon 连接到 Telegram。

from telethon import TelegramClient
import logging

api_id = 1111111
api_hash = 'd0111111111111111111111111111111'

client = TelegramClient('session_name', api_id, api_hash)

logging.basicConfig(level=logging.DEBUG)

client.start()

从日志来看,似乎会连接并立即断开连接。 Telegram 在我的国家(捷克共和国)没有被屏蔽,我通常使用桌面客户​​端,没有任何 VPN/proxy。我试过 Python 3.7 和 Python 3.9.

查看下面的日志。

INFO:telethon.network.mtprotosender:Connecting to 149.154.167.51:443/TcpFull...
DEBUG:telethon.network.mtprotosender:Connection attempt 1...
DEBUG:telethon.network.mtprotosender:Connection success!
DEBUG:telethon.network.mtprotosender:New auth_key attempt 1...
INFO:telethon.network.connection.connection:The server closed the connection
WARNING:telethon.network.mtprotosender:Connection error 1 during auth_key gen: ConnectionError: Not connected
DEBUG:telethon.network.mtprotosender:Connection attempt 2...
DEBUG:telethon.network.mtprotosender:Connection success!
DEBUG:telethon.network.mtprotosender:New auth_key attempt 2...
INFO:telethon.network.connection.connection:The server closed the connection
WARNING:telethon.network.mtprotosender:Connection error 2 during auth_key gen: ConnectionError: Not connected
DEBUG:telethon.network.mtprotosender:Connection attempt 3...
DEBUG:telethon.network.mtprotosender:Connection success!
DEBUG:telethon.network.mtprotosender:New auth_key attempt 3...
INFO:telethon.network.connection.connection:The server closed the connection
WARNING:telethon.network.mtprotosender:Connection error 3 during auth_key gen: ConnectionError: Not connected
DEBUG:telethon.network.mtprotosender:Connection attempt 4...
DEBUG:telethon.network.mtprotosender:Connection success!
DEBUG:telethon.network.mtprotosender:New auth_key attempt 4...
INFO:telethon.network.connection.connection:The server closed the connection
WARNING:telethon.network.mtprotosender:Connection error 4 during auth_key gen: ConnectionError: Not connected
DEBUG:telethon.network.mtprotosender:Connection attempt 5...
DEBUG:telethon.network.mtprotosender:Connection success!
DEBUG:telethon.network.mtprotosender:New auth_key attempt 5...
INFO:telethon.network.connection.connection:The server closed the connection
WARNING:telethon.network.mtprotosender:Connection error 5 during auth_key gen: ConnectionError: Not connected
DEBUG:telethon.network.mtprotosender:Connection attempt 6...
DEBUG:telethon.network.mtprotosender:Connection success!
DEBUG:telethon.network.mtprotosender:New auth_key attempt 6...
INFO:telethon.network.connection.connection:The server closed the connection
WARNING:telethon.network.mtprotosender:Connection error 6 during auth_key gen: ConnectionError: Not connected
Traceback (most recent call last):
  File "c:\Users\username\Documents\Python\telegram_read_messages.py", line 11, in <module>
    client.start()
  File "C:\Users\username\AppData\Local\Programs\Python\Python39\lib\site-packages\telethon\client\auth.py", line 133, in start
    else self.loop.run_until_complete(coro)
  File "C:\Users\username\AppData\Local\Programs\Python\Python39\lib\asyncio\base_events.py", line 642, in run_until_complete
    return future.result()
  File "C:\Users\username\AppData\Local\Programs\Python\Python39\lib\site-packages\telethon\client\auth.py", line 140, in _start
    await self.connect()
  File "C:\Users\username\AppData\Local\Programs\Python\Python39\lib\site-packages\telethon\client\telegrambaseclient.py", line 513, in connect
    if not await self._sender.connect(self._connection(
  File "C:\Users\username\AppData\Local\Programs\Python\Python39\lib\site-packages\telethon\network\mtprotosender.py", line 127, in connect
    await self._connect()
  File "C:\Users\username\AppData\Local\Programs\Python\Python39\lib\site-packages\telethon\network\mtprotosender.py", line 253, in _connect
    raise ConnectionError('Connection to Telegram failed {} time(s)'.format(self._retries))
ConnectionError: Connection to Telegram failed 5 time(s)

我的最终目标是能够简单地连接和阅读来自我所属的群组的消息。

感谢任何提示。

尝试按照以下步骤操作: https://docs.telethon.dev/en/latest/

像这样:

from telethon.sync import TelegramClient, events

with TelegramClient('name', api_id, api_hash) as client:
   client.send_message('me', 'Hello, myself!')
   print(client.download_profile_photo('me'))

   @client.on(events.NewMessage(pattern='(?i).*Hello'))
   async def handler(event):
      await event.reply('Hey!')

   client.run_until_disconnected()

感谢您提出的所有建议。最后,我的原始代码完全没问题。

问题与网络有关。