mirror of
https://github.com/python-telegram-bot/python-telegram-bot.git
synced 2024-11-22 15:17:00 +01:00
Add support for Socks5 proxy. (#518)
This commit is contained in:
parent
0507378509
commit
e39afad321
2 changed files with 15 additions and 5 deletions
1
setup.py
1
setup.py
|
@ -36,6 +36,7 @@ with codecs.open('README.rst', 'r', 'utf-8') as fd:
|
||||||
install_requires=requirements(),
|
install_requires=requirements(),
|
||||||
extras_require={
|
extras_require={
|
||||||
'json': 'ujson',
|
'json': 'ujson',
|
||||||
|
'socks': 'PySocks'
|
||||||
},
|
},
|
||||||
include_package_data=True,
|
include_package_data=True,
|
||||||
classifiers=[
|
classifiers=[
|
||||||
|
|
|
@ -30,6 +30,10 @@ import certifi
|
||||||
import urllib3
|
import urllib3
|
||||||
from urllib3.connection import HTTPConnection
|
from urllib3.connection import HTTPConnection
|
||||||
from urllib3.util.timeout import Timeout
|
from urllib3.util.timeout import Timeout
|
||||||
|
try:
|
||||||
|
from urllib3.contrib.socks import SOCKSProxyManager
|
||||||
|
except ImportError:
|
||||||
|
SOCKSProxyManager = None
|
||||||
|
|
||||||
from telegram import (InputFile, TelegramError)
|
from telegram import (InputFile, TelegramError)
|
||||||
from telegram.error import (Unauthorized, NetworkError, TimedOut, BadRequest, ChatMigrated,
|
from telegram.error import (Unauthorized, NetworkError, TimedOut, BadRequest, ChatMigrated,
|
||||||
|
@ -92,11 +96,16 @@ class Request(object):
|
||||||
mgr = urllib3.PoolManager(**kwargs)
|
mgr = urllib3.PoolManager(**kwargs)
|
||||||
else:
|
else:
|
||||||
kwargs.update(urllib3_proxy_kwargs)
|
kwargs.update(urllib3_proxy_kwargs)
|
||||||
mgr = urllib3.proxy_from_url(proxy_url, **kwargs)
|
if proxy_url.startswith('socks'):
|
||||||
if mgr.proxy.auth:
|
if not SOCKSProxyManager:
|
||||||
# TODO: what about other auth types?
|
raise RuntimeError('PySocks is missing')
|
||||||
auth_hdrs = urllib3.make_headers(proxy_basic_auth=mgr.proxy.auth)
|
mgr = SOCKSProxyManager(proxy_url, **kwargs)
|
||||||
mgr.proxy_headers.update(auth_hdrs)
|
else:
|
||||||
|
mgr = urllib3.proxy_from_url(proxy_url, **kwargs)
|
||||||
|
if mgr.proxy.auth:
|
||||||
|
# TODO: what about other auth types?
|
||||||
|
auth_hdrs = urllib3.make_headers(proxy_basic_auth=mgr.proxy.auth)
|
||||||
|
mgr.proxy_headers.update(auth_hdrs)
|
||||||
|
|
||||||
self._con_pool = mgr
|
self._con_pool = mgr
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue