mirror of
https://github.com/LonamiWebs/Telethon.git
synced 2024-11-25 19:03:46 +03:00
Add proxy support using PySocks (closes #37)
This commit is contained in:
parent
b4c81a60b3
commit
350c11d66f
16
README.rst
16
README.rst
|
@ -171,3 +171,19 @@ and replacing the one you can find in this same directory by the updated one.
|
||||||
Don't forget to run ``python3 tl_generator.py``.
|
Don't forget to run ``python3 tl_generator.py``.
|
||||||
|
|
||||||
If the changes weren't too big, everything should still work the same way as it did before; but with extra features.
|
If the changes weren't too big, everything should still work the same way as it did before; but with extra features.
|
||||||
|
|
||||||
|
Using proxy
|
||||||
|
-----------
|
||||||
|
If you want to use Telethon via proxy, you have to install
|
||||||
|
`PySocks (via pip or manual) <https://github.com/Anorov/PySocks#installation>`_
|
||||||
|
and pass proxy settings to ``TelegramClient()`` like
|
||||||
|
|
||||||
|
.. code:: python
|
||||||
|
|
||||||
|
>>> from telethon import InteractiveTelegramClient
|
||||||
|
>>> import socks
|
||||||
|
>>> client = InteractiveTelegramClient('sessionid', '+34600000000',
|
||||||
|
... api_id=12345, api_hash='0123456789abcdef0123456789abcdef', proxy=(socks.SOCKS5, "localhost", 4444))
|
||||||
|
|
||||||
|
``proxy`` accept a tuple, which contains exactly the same parameters as
|
||||||
|
`what a set_proxy method accepts <https://github.com/Anorov/PySocks#sockssocksocket>`_.
|
||||||
|
|
|
@ -30,11 +30,11 @@ def bytes_to_string(byte_count):
|
||||||
|
|
||||||
|
|
||||||
class InteractiveTelegramClient(TelegramClient):
|
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_title('Initialization')
|
||||||
|
|
||||||
print('Initializing interactive example...')
|
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,
|
# Store all the found media in memory here,
|
||||||
# so it can be downloaded if the user wants
|
# so it can be downloaded if the user wants
|
||||||
|
|
|
@ -9,8 +9,19 @@ from telethon.utils import BinaryWriter
|
||||||
|
|
||||||
|
|
||||||
class TcpClient:
|
class TcpClient:
|
||||||
def __init__(self):
|
def __init__(self, proxy=None):
|
||||||
self.connected = False
|
self.connected = False
|
||||||
|
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)
|
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||||
|
|
||||||
# Support for multi-threading advantages and safety
|
# Support for multi-threading advantages and safety
|
||||||
|
|
|
@ -7,8 +7,8 @@ from telethon.utils import BinaryWriter
|
||||||
|
|
||||||
|
|
||||||
class TcpTransport:
|
class TcpTransport:
|
||||||
def __init__(self, ip_address, port):
|
def __init__(self, ip_address, port, proxy=None):
|
||||||
self.tcp_client = TcpClient()
|
self.tcp_client = TcpClient(proxy)
|
||||||
self.send_counter = 0
|
self.send_counter = 0
|
||||||
|
|
||||||
self.tcp_client.connect(ip_address, port)
|
self.tcp_client.connect(ip_address, port)
|
||||||
|
|
|
@ -45,7 +45,7 @@ class TelegramClient:
|
||||||
|
|
||||||
# region Initialization
|
# 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.
|
"""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)
|
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.')
|
'The given session must either be a string or a Session instance.')
|
||||||
|
|
||||||
self.transport = TcpTransport(self.session.server_address,
|
self.transport = TcpTransport(self.session.server_address,
|
||||||
self.session.port)
|
self.session.port, proxy)
|
||||||
|
|
||||||
# These will be set later
|
# These will be set later
|
||||||
self.dc_options = None
|
self.dc_options = None
|
||||||
|
|
Loading…
Reference in New Issue
Block a user