Add support for Socks5 proxy. (#518)

This commit is contained in:
代码家 2017-02-26 02:47:56 +08:00 committed by Noam Meltzer
parent 0507378509
commit e39afad321
2 changed files with 15 additions and 5 deletions

View file

@ -36,6 +36,7 @@ with codecs.open('README.rst', 'r', 'utf-8') as fd:
install_requires=requirements(),
extras_require={
'json': 'ujson',
'socks': 'PySocks'
},
include_package_data=True,
classifiers=[

View file

@ -30,6 +30,10 @@ import certifi
import urllib3
from urllib3.connection import HTTPConnection
from urllib3.util.timeout import Timeout
try:
from urllib3.contrib.socks import SOCKSProxyManager
except ImportError:
SOCKSProxyManager = None
from telegram import (InputFile, TelegramError)
from telegram.error import (Unauthorized, NetworkError, TimedOut, BadRequest, ChatMigrated,
@ -92,11 +96,16 @@ class Request(object):
mgr = urllib3.PoolManager(**kwargs)
else:
kwargs.update(urllib3_proxy_kwargs)
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)
if proxy_url.startswith('socks'):
if not SOCKSProxyManager:
raise RuntimeError('PySocks is missing')
mgr = SOCKSProxyManager(proxy_url, **kwargs)
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