Telethon: python 3.8 proxy bug

Created on 28 Nov 2019  路  16Comments  路  Source: LonamiWebs/Telethon

Hi everyone.

Running python64 3.8 under Windows 10

proxy = (socks.SOCKS5, '1.1.1.1', 1111)
client = TelegramClient(sessions_folder + phone, api_id, api_hash, proxy=proxy)
client.connect()

I am able to connect despite using not valid proxy, hence the proxy is not used. Under python 3.7 i get expected "can't connect" response.

Most helpful comment

I did some research. ProactorEventLoop causing the proxy to be ignored.
In python 3.8 changelog:

on Windows, the default asyncio event loop is now ProactorEventLoop

Setting event loop to SelectorEventLoop fixes the issue.

asyncio.set_event_loop(asyncio.SelectorEventLoop())

All 16 comments

I'm having similar issue: client ignores proxy settings, it's trying to connect to servers directly.

3.8 32bit

I did some research. ProactorEventLoop causing the proxy to be ignored.
In python 3.8 changelog:

on Windows, the default asyncio event loop is now ProactorEventLoop

Setting event loop to SelectorEventLoop fixes the issue.

asyncio.set_event_loop(asyncio.SelectorEventLoop())

Do you know what the implications of that are, and what was the full traceback previously?

If i understood correctly ProactorEventLoop is missing 'sock_connect' method used here

https://github.com/LonamiWebs/Telethon/blob/38b929b973c3603e55d4cfd67232baee095b1a4c/telethon/network/connection/connection.py#L70

Thanks for investigating, until a proper fix is released, this is better than nothing. Force pushing another commit with a typo fixed.

That fix isnt enough. see my comment on the commit

It's not a fix, it's just a way to warn users about a possible workaround.

i know. but it wont warn the users on 3.7. see my comment

Users on 3.7 with ProactorLoop will also lack the method we need, won't they? Or was it just removed in 3.8?

Yeah, they'll lack it too
But look:
if (proxy is not None and proactor is not None and sys.version_info >= (3, 8) and isinstance(self._loop, proactor)):

The warning wont show on 3.7, but its possible to set event loop manually on 3.7, to fix subprocesses on windows (my code does)

Oh, understood, that makes sense. So we should simply remove the sys.version_info check, you say?

Yes

asyncio.set_event_loop(asyncio.SelectorEventLoop())

where should I put this? I put this in my telegram client but it still wont connect. So im on 3.7 =(

ok. I should put it not in def main, but right after #import statements

@ism

_Running python64 3.8 under Windows 10_
_Under python 3.7 I get expected "can't connect" response._

proxy = (socks.SOCKS5, '1.1.1.1', 1111)
client = TelegramClient(sessions_folder + phone, api_id, api_hash, proxy=proxy)

@lokkiser

_python32 3.8 - similar issue: client ignores proxy settings, it's trying to connect to servers directly_


@DaveScream

_I put this in my telegram client but it still wont connect. So im on 3.7 =(_


@penn5, @Lonami

  1. I hope you guys aware of this commit as per fixing their bug: https://bugs.python.org/issue33792
    https://github.com/python/cpython/commit/0738443a5b071a6bd2c18957a06cfe571a7314f2

Windows users might have fails due WindowsSelectorEventLoopPolicy

  1. I had no issues myself in using PySocks. Works like a charm in 3.7/3.8 Win 7, 10 / *Nix
import socks
client = TelegramClient(session_name, api_id, api_hash, proxy=(socks.SOCKS5, 'SOCKS5_PROXY_IP', SOCKS5_PROXY_PORT, True, 'SOCKS5_PROXY_USERNAME', 'SOCKS5_PROXY_PASSWORD'))

@x0x8x that's an issue. It does work when it should not. :-)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rbabaee2014 picture rbabaee2014  路  3Comments

ghost picture ghost  路  4Comments

JackCloudman picture JackCloudman  路  6Comments

Fleyderer picture Fleyderer  路  3Comments

ksanderer picture ksanderer  路  3Comments