From ca6f8b9ced76924ea1d4e1c568805d6e86afef24 Mon Sep 17 00:00:00 2001 From: nibrag Date: Sun, 12 Nov 2017 17:31:52 +0300 Subject: [PATCH] aiohttp dependence check --- aiosocks/__init__.py | 2 +- aiosocks/connector.py | 27 ++++++++++++++++++--------- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/aiosocks/__init__.py b/aiosocks/__init__.py index 1726d82..1f13d8b 100644 --- a/aiosocks/__init__.py +++ b/aiosocks/__init__.py @@ -8,7 +8,7 @@ from .helpers import ( ) from .protocols import Socks4Protocol, Socks5Protocol, DEFAULT_LIMIT -__version__ = '0.2.4' +__version__ = '0.2.5' __all__ = ('Socks4Protocol', 'Socks5Protocol', 'Socks4Auth', 'Socks5Auth', 'Socks4Addr', 'Socks5Addr', 'SocksError', diff --git a/aiosocks/connector.py b/aiosocks/connector.py index 2fbd13f..afdaea8 100644 --- a/aiosocks/connector.py +++ b/aiosocks/connector.py @@ -1,18 +1,22 @@ -from aiohttp.client_exceptions import certificate_errors, ssl_errors - try: import aiohttp from aiohttp.connector import sentinel + from aiohttp.client_exceptions import certificate_errors, ssl_errors except ImportError: raise ImportError('aiosocks.SocksConnector require aiohttp library') - -from .errors import SocksError, SocksConnectionError +from .errors import SocksConnectionError from .helpers import Socks4Auth, Socks5Auth, Socks4Addr, Socks5Addr from . import create_connection __all__ = ('ProxyConnector', 'ProxyClientRequest') +from distutils.version import StrictVersion + +if StrictVersion(aiohttp.__version__) < StrictVersion('2.3.2'): + raise RuntimeError('aiosocks.connector depends on aiohttp 2.3.2+') + + class ProxyClientRequest(aiohttp.ClientRequest): def update_proxy(self, proxy, proxy_auth, proxy_headers): if proxy and proxy.scheme not in ['http', 'socks4', 'socks5']: @@ -66,9 +70,11 @@ class ProxyConnector(aiohttp.TCPConnector): raise aiohttp.ClientConnectorCertificateError( req.connection_key, exc) from exc except ssl_errors as exc: - raise aiohttp.ClientConnectorSSLError(req.connection_key, exc) from exc + raise aiohttp.ClientConnectorSSLError( + req.connection_key, exc) from exc except (OSError, SocksConnectionError) as exc: - raise aiohttp.ClientProxyConnectionError(req.connection_key, exc) from exc + raise aiohttp.ClientProxyConnectionError( + req.connection_key, exc) from exc async def _create_socks_connection(self, req): sslcontext = self._get_ssl_context(req) @@ -79,14 +85,17 @@ class ProxyConnector(aiohttp.TCPConnector): dst_hosts = list(await self._resolve_host(req.host, req.port)) dst = dst_hosts[0]['host'], dst_hosts[0]['port'] except OSError as exc: - raise aiohttp.ClientConnectorError(req.connection_key, exc) from exc + raise aiohttp.ClientConnectorError( + req.connection_key, exc) from exc else: dst = req.host, req.port try: - proxy_hosts = await self._resolve_host(req.proxy.host, req.proxy.port) + proxy_hosts = await self._resolve_host( + req.proxy.host, req.proxy.port) except OSError as exc: - raise aiohttp.ClientConnectorError(req.connection_key, exc) from exc + raise aiohttp.ClientConnectorError( + req.connection_key, exc) from exc last_exc = None