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(), install_requires=requirements(),
extras_require={ extras_require={
'json': 'ujson', 'json': 'ujson',
'socks': 'PySocks'
}, },
include_package_data=True, include_package_data=True,
classifiers=[ classifiers=[

View file

@ -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,6 +96,11 @@ class Request(object):
mgr = urllib3.PoolManager(**kwargs) mgr = urllib3.PoolManager(**kwargs)
else: else:
kwargs.update(urllib3_proxy_kwargs) kwargs.update(urllib3_proxy_kwargs)
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) mgr = urllib3.proxy_from_url(proxy_url, **kwargs)
if mgr.proxy.auth: if mgr.proxy.auth:
# TODO: what about other auth types? # TODO: what about other auth types?