Search code examples
pythontelegramtelethontxt

Connecting a proxy to a Python script using the Telethon module


I need to get the proxy from the proxy.txt file:

ip:port:username:password

and add to the code:

file = dbm_base()
api_id = int(file['api_id4'].decode())
api_hash = file['api_hash4'].decode()
client = TelegramClient('client4', api_id, api_hash, proxy=(socks.SOCKS5, 'ip', port, 'username', 'password'))#port without ''

I doing this:

with open('proxy.txt', 'r') as f:
    proxys = f.readline().split(":")

file = dbm_base()
api_id = int(file['api_id4'].decode())
api_hash = file['api_hash4'].decode()
s = socks.socksocket()
client = TelegramClient('client1', api_id, api_hash, proxy=s.set_proxy(socks.HTTP, f'{proxys[0]}', int(proxys[1]), f'{proxys[2]}', f'{proxys[3]}'))

But the proxy server does not connect to the script.

What did I do wrong and why is the proxy server not connecting?


Solution

  • Not sure about HTTP proxy or others, but you can use MTPROTO proxy with super ease:

    
    server = 'firewall.firewall-gw.cam'           # TODO: proxy server or ip
    port = 443                                    # TODO: set port, normally 443
    secret = 'dd00000000000000000000000000000000' # TODO: set proxy secret, normally hex encoded
    
    connection = connection.ConnectionTcpMTProxyRandomizedIntermediate # this mode supports most proxies
    client = TelegramClient('client1', api_id, api_hash, connection=connection,proxy=(server, port, secret)))
    

    MTPROXY can be took from @ProxyMTProto. (Note: Its open source here)

    The problem with other public proxies is that, they expire frequently.

    Learn more here.