Telethon: Http proxy

Created on 5 Dec 2017  路  10Comments  路  Source: LonamiWebs/Telethon


Have a timout problem with client.connect()

Versions:
ubuntu 16.04
python 3.5.2
telethon 0.15.5.7

Code:
api_id = XXXXXX
api_hash = '**********'
phone = '+XXXXXXXXXXX'

host = 'XXX.XX.X.XXX' # valid proxy
port = XXXX # valid port
proxy = (socks.HTTP, host, port)

client = TelegramClient('test', api_id, api_hash, proxy, timeout=timedelta(seconds=30))
client.connect()

gives me "socket.timeout: timed out". I have tried proxy=None also, since $http_proxy is defined in linux environments (package "requests" work perfectly with it). And I had the same error.

Do you have idea how to check or fix it?
Thanks

Most helpful comment

@Lonami Thanks the code works but the TelegramClient still can not connect as I am using VPN proxy

All 10 comments

Do you have idea how to check or fix it?

Not really. I don't work with proxies.

Yeah, i read it in other issues) Nevertheless many thanks for your answer. I forgot to write i have ipv4 proxy. And i'll be happy for any help.

socks.set_default_proxy(socks.SOCKS5, addr="localhost", port=1080)
socket.socket = socks.socksocket
socket.setdefaulttimeout(2.0)
api_id = xxxx
api_hash = 'xxxx'
client = TelegramClient('xxx', api_id, api_hash)
client.start()

Just tested this code and it works well

proxy = (socks.HTTP, proxy_host, proxy_port,)
client = TelegramClient(phone_number, api_id, api_hash, proxy=proxy)
client.connect()

Proxies only supporting HTTP should work after https://github.com/LonamiWebs/Telethon/commit/128053750d6cf504c8aa368cfddff36dfa3aef46, which you can test right now if you run:

pip install --upgrade git+https://github.com/LonamiWebs/Telethon@master

With something like:

from telethon import TelegramClient, sync
from telethon.network import ConnectionHttp

client = TelegramClient(..., connection=ConnectionHttp)
with client:
    client.send_message('me', 'Hi!')

It would be nice if someone could report whether proxies work with this or not (of course, pass a proxy= argument when creating the client, too).

@Lonami when I try the ConntectionHttp , it says missing 1 required keyword-only argument: 'loop'

Version? Traceback?

@Lonami I use the telethon-sync version

Yeah right I just pushed a fix.

@Lonami Thanks the code works but the TelegramClient still can not connect as I am using VPN proxy

Was this page helpful?
0 / 5 - 0 ratings