【问题标题】:Telethon - The server closed the connection errorTelethon - 服务器关闭连接错误
【发布时间】:2022-01-13 21:19:45
【问题描述】:

我无法使用 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/代理。我尝试了 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)

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

感谢任何提示。

【问题讨论】:

    标签: python telethon


    【解决方案1】:

    尝试按照以下步骤操作: 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()
    

    【讨论】:

    • 感谢您的回复。不幸的是,它不起作用。我遇到了同样的错误。
    • 你需要使用telethon而不是telethon.sync然后去掉await。
    【解决方案2】:

    感谢您的所有建议。最后,我的原始代码非常好。

    问题与网络有关。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-08
      • 2017-06-29
      • 2013-12-11
      • 1970-01-01
      • 1970-01-01
      • 2012-10-12
      • 2020-06-04
      • 1970-01-01
      相关资源
      最近更新 更多