X Tutup
Skip to content

Commit e39afad

Browse files
daimajiatsnoam
authored andcommitted
Add support for Socks5 proxy. (python-telegram-bot#518)
1 parent 0507378 commit e39afad

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ def requirements():
3636
install_requires=requirements(),
3737
extras_require={
3838
'json': 'ujson',
39+
'socks': 'PySocks'
3940
},
4041
include_package_data=True,
4142
classifiers=[

telegram/utils/request.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@
3030
import urllib3
3131
from urllib3.connection import HTTPConnection
3232
from urllib3.util.timeout import Timeout
33+
try:
34+
from urllib3.contrib.socks import SOCKSProxyManager
35+
except ImportError:
36+
SOCKSProxyManager = None
3337

3438
from telegram import (InputFile, TelegramError)
3539
from telegram.error import (Unauthorized, NetworkError, TimedOut, BadRequest, ChatMigrated,
@@ -92,11 +96,16 @@ def __init__(self,
9296
mgr = urllib3.PoolManager(**kwargs)
9397
else:
9498
kwargs.update(urllib3_proxy_kwargs)
95-
mgr = urllib3.proxy_from_url(proxy_url, **kwargs)
96-
if mgr.proxy.auth:
97-
# TODO: what about other auth types?
98-
auth_hdrs = urllib3.make_headers(proxy_basic_auth=mgr.proxy.auth)
99-
mgr.proxy_headers.update(auth_hdrs)
99+
if proxy_url.startswith('socks'):
100+
if not SOCKSProxyManager:
101+
raise RuntimeError('PySocks is missing')
102+
mgr = SOCKSProxyManager(proxy_url, **kwargs)
103+
else:
104+
mgr = urllib3.proxy_from_url(proxy_url, **kwargs)
105+
if mgr.proxy.auth:
106+
# TODO: what about other auth types?
107+
auth_hdrs = urllib3.make_headers(proxy_basic_auth=mgr.proxy.auth)
108+
mgr.proxy_headers.update(auth_hdrs)
100109

101110
self._con_pool = mgr
102111

0 commit comments

Comments
 (0)
X Tutup