mirror of
https://github.com/LonamiWebs/Telethon.git
synced 2026-09-08 01:41:59 +00:00
Add proxy support using PySocks (closes #37)
This commit is contained in:
@@ -30,11 +30,11 @@ def bytes_to_string(byte_count):
|
||||
|
||||
|
||||
class InteractiveTelegramClient(TelegramClient):
|
||||
def __init__(self, session_user_id, user_phone, api_id, api_hash):
|
||||
def __init__(self, session_user_id, user_phone, api_id, api_hash, proxy=None):
|
||||
print_title('Initialization')
|
||||
|
||||
print('Initializing interactive example...')
|
||||
super().__init__(session_user_id, api_id, api_hash)
|
||||
super().__init__(session_user_id, api_id, api_hash, proxy)
|
||||
|
||||
# Store all the found media in memory here,
|
||||
# so it can be downloaded if the user wants
|
||||
|
||||
@@ -9,9 +9,20 @@ from telethon.utils import BinaryWriter
|
||||
|
||||
|
||||
class TcpClient:
|
||||
def __init__(self):
|
||||
def __init__(self, proxy=None):
|
||||
self.connected = False
|
||||
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
if proxy:
|
||||
try:
|
||||
import socks
|
||||
self.socket = socks.socksocket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
self.socket.set_proxy(*proxy)
|
||||
except (ImportError, SystemError):
|
||||
print("Can't import PySocks, fallback to vanilla socket. "
|
||||
"Proxy settings are ignored. "
|
||||
"Try to install PySocks via pip")
|
||||
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
else:
|
||||
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
|
||||
# Support for multi-threading advantages and safety
|
||||
self.cancelled = False # Has the read operation been cancelled?
|
||||
|
||||
@@ -7,8 +7,8 @@ from telethon.utils import BinaryWriter
|
||||
|
||||
|
||||
class TcpTransport:
|
||||
def __init__(self, ip_address, port):
|
||||
self.tcp_client = TcpClient()
|
||||
def __init__(self, ip_address, port, proxy=None):
|
||||
self.tcp_client = TcpClient(proxy)
|
||||
self.send_counter = 0
|
||||
|
||||
self.tcp_client.connect(ip_address, port)
|
||||
|
||||
@@ -45,7 +45,7 @@ class TelegramClient:
|
||||
|
||||
# region Initialization
|
||||
|
||||
def __init__(self, session, api_id, api_hash):
|
||||
def __init__(self, session, api_id, api_hash, proxy=None):
|
||||
"""Initializes the Telegram client with the specified API ID and Hash.
|
||||
|
||||
Session can either be a `str` object (the filename for the loaded/saved .session)
|
||||
@@ -72,7 +72,7 @@ class TelegramClient:
|
||||
'The given session must either be a string or a Session instance.')
|
||||
|
||||
self.transport = TcpTransport(self.session.server_address,
|
||||
self.session.port)
|
||||
self.session.port, proxy)
|
||||
|
||||
# These will be set later
|
||||
self.dc_options = None
|
||||
|
||||
Reference in New Issue
Block a user