【发布时间】: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)
我的最终目标是能够简单地连接和阅读来自我所属的群组的消息。
感谢任何提示。
【问题讨论】: