LonamiWebs / Telethon

Pure Python 3 MTProto API Telegram client library, for bots too!
https://docs.telethon.dev
MIT License
10.06k stars 1.41k forks source link

Use the same proxy Telethon can not connect Telegram but Pyrogram work #4505

Open hyperbolic-c opened 2 weeks ago

hyperbolic-c commented 2 weeks ago

Code that causes the issue

from telethon import TelegramClient, events, types
from telethon.sessions import StringSession

session = StringSession()
client = TelegramClient(session, api_id, api_hash, proxy=teleProxy)

client.start(phone=phone_number)
async def main():
    me = await client.get_me()
    print(me.stringify())
with client:
    client.loop.run_until_complete(main())

Expected behavior

I am going to connect Telegram using Telethon with a proxy in a .py file, but always get a error: ConnectionError: Connection to Telegram failed 5 time(s). When I used Jupyter notebook it works. Then I try to use Pyrogram with the same proxy, it can connect to Telegram !!! And my desktop Telegram work, too. So it is just Telethon get a error.

Actual behavior

Attempt 4 at connecting failed: ProxyConnectionError: Error connecting to HTTP proxy 127.0.0.1:7897: [WinError 10061] 
Attempt 5 at connecting failed: ProxyConnectionError: Error connecting to HTTP proxy 127.0.0.1:7897: [WinError 10061] 
Attempt 6 at connecting failed: ProxyConnectionError: Error connecting to HTTP proxy 127.0.0.1:7897: [WinError 10061] 
Automatic reconnection failed 5 time(s)

Traceback

No response

Telethon version

1.37

Python version

3.11

Operating system (including distribution name and version)

Windows 11

Other details

No response

Checklist

ebrahimrb commented 4 hours ago

Hi , Just install python_socks , I had the same issue. This library uses two libraries for connecting to a proxy. When python_socks is installed, it uses that, and everything works fine without any problems. However, when it uses the other library, socks, it faces issues, and the proxy isn't applied—it gets bypassed. I couldn’t figure out why this happens, as my knowledge isn’t deep enough to understand the code at that level.

pip install python_socks