Skip to content

Commit

Permalink
Fixed: Dependent on the "aiohttp" must be imported only in aiosocks.c…
Browse files Browse the repository at this point in the history
…onnector
  • Loading branch information
nibrag committed Jun 15, 2016
1 parent 84bc8e5 commit d714053
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 45 deletions.
8 changes: 5 additions & 3 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,9 @@ aiohttp usage
import asyncio
import aiohttp
import aiosocks
from aiosocks.connector import SocksConnector, proxy_connector
from aiosocks.connector import (
SocksConnector, proxy_connector, HttpProxyAddr, HttpProxyAuth
)
async def load_github_main():
Expand All @@ -154,8 +156,8 @@ aiohttp usage
remote_resolve=True, verify_ssl=False)
# return SocksConnector
conn = proxy_connector(aiosocks.HttpProxyAddr('http://proxy'),
aiosocks.HttpProxyAuth('login', 'pwd'))
conn = proxy_connector(HttpProxyAddr('http://proxy'),
HttpProxyAuth('login', 'pwd'))
# return aiohttp.ProxyConnector (http proxy connector)
try:
Expand Down
12 changes: 5 additions & 7 deletions aiosocks/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,17 @@
SocksConnectionError, InvalidServerReply, InvalidServerVersion
)
from .helpers import (
SocksAddr, Socks4Addr, Socks5Addr, Socks4Auth,
Socks5Auth, HttpProxyAddr, HttpProxyAuth
SocksAddr, Socks4Addr, Socks5Addr, Socks4Auth, Socks5Auth
)
from .protocols import Socks4Protocol, Socks5Protocol, DEFAULT_LIMIT

__version__ = '0.1.5'

__all__ = ('Socks4Protocol', 'Socks5Protocol', 'Socks4Auth',
'Socks5Auth', 'Socks4Addr', 'Socks5Addr', 'HttpProxyAddr',
'HttpProxyAuth', 'SocksError', 'NoAcceptableAuthMethods',
'LoginAuthenticationFailed', 'SocksConnectionError',
'InvalidServerVersion', 'InvalidServerReply',
'create_connection', 'open_connection')
'Socks5Auth', 'Socks4Addr', 'Socks5Addr', 'SocksError',
'NoAcceptableAuthMethods', 'LoginAuthenticationFailed',
'SocksConnectionError', 'InvalidServerVersion',
'InvalidServerReply', 'create_connection', 'open_connection')


@asyncio.coroutine
Expand Down
21 changes: 17 additions & 4 deletions aiosocks/connector.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,24 @@
try:
import aiohttp
from aiohttp.errors import ProxyConnectionError
from aiohttp.helpers import BasicAuth as HttpProxyAuth
except ImportError:
raise ImportError('aiosocks.SocksConnector require aiohttp library')

import asyncio
import aiohttp
from aiohttp.errors import ProxyConnectionError
from collections import namedtuple
from .errors import SocksError, SocksConnectionError
from .helpers import HttpProxyAddr, SocksAddr
from .helpers import SocksAddr
from . import create_connection

__all__ = ('SocksConnector',)
__all__ = ('SocksConnector', 'HttpProxyAddr', 'HttpProxyAuth')


class HttpProxyAddr(namedtuple('HttpProxyAddr', ['url'])):
def __new__(cls, url):
if url is None:
raise ValueError('None is not allowed as url value')
return super().__new__(cls, url)


class SocksConnector(aiohttp.TCPConnector):
Expand Down
11 changes: 1 addition & 10 deletions aiosocks/helpers.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
from collections import namedtuple
from aiohttp.helpers import BasicAuth as HttpProxyAuth

__all__ = ('Socks4Auth', 'Socks5Auth', 'Socks4Addr', 'Socks5Addr', 'SocksAddr',
'HttpProxyAddr', 'HttpProxyAuth')
__all__ = ('Socks4Auth', 'Socks5Auth', 'Socks4Addr', 'Socks5Addr', 'SocksAddr')


class Socks4Auth(namedtuple('Socks4Auth', ['login', 'encoding'])):
Expand Down Expand Up @@ -43,10 +41,3 @@ class Socks4Addr(SocksAddr):

class Socks5Addr(SocksAddr):
pass


class HttpProxyAddr(namedtuple('HttpProxyAddr', ['url'])):
def __new__(cls, url):
if url is None:
raise ValueError('None is not allowed as url value')
return super().__new__(cls, url)
31 changes: 18 additions & 13 deletions tests/test_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@
import asyncio
import aiosocks
import aiohttp
import pytest
from unittest import mock
from aiohttp.client_reqrep import ClientRequest
from aiosocks.connector import SocksConnector, proxy_connector
from aiosocks.connector import SocksConnector, proxy_connector, HttpProxyAddr
from .helpers import fake_coroutine


Expand Down Expand Up @@ -133,18 +132,24 @@ def test_proxy_negotiate_fail(self, cr_conn_mock):
with self.assertRaises(aiosocks.SocksError):
self.loop.run_until_complete(connector.connect(req))

def test_proxy_connector(self):
socks4_addr = aiosocks.Socks4Addr('h')
socks5_addr = aiosocks.Socks5Addr('h')
http_addr = HttpProxyAddr('http://proxy')

def test_proxy_connector():
socks4_addr = aiosocks.Socks4Addr('h')
socks5_addr = aiosocks.Socks5Addr('h')
http_addr = aiosocks.HttpProxyAddr('http://proxy')
self.assertIsInstance(proxy_connector(socks4_addr, loop=self.loop),
SocksConnector)
self.assertIsInstance(proxy_connector(socks5_addr, loop=self.loop),
SocksConnector)
self.assertIsInstance(proxy_connector(http_addr, loop=self.loop),
aiohttp.ProxyConnector)

loop = asyncio.new_event_loop()
with self.assertRaises(ValueError):
proxy_connector(None)

assert isinstance(proxy_connector(socks4_addr, loop=loop), SocksConnector)
assert isinstance(proxy_connector(socks5_addr, loop=loop), SocksConnector)
assert isinstance(proxy_connector(http_addr, loop=loop),
aiohttp.ProxyConnector)
def test_http_proxy_addr(self):
addr = HttpProxyAddr('http://proxy')
self.assertEqual(addr.url, 'http://proxy')

with pytest.raises(ValueError):
proxy_connector(None)
with self.assertRaises(ValueError):
HttpProxyAddr(None)
8 changes: 0 additions & 8 deletions tests/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,3 @@ def test_socks5_addr4():
addr = aiosocks.Socks5Addr('localhost', None)
assert addr.host == 'localhost'
assert addr.port == 1080


def test_http_proxy_addr():
addr = aiosocks.HttpProxyAddr('http://proxy')
assert addr.url == 'http://proxy'

with pytest.raises(ValueError):
aiosocks.HttpProxyAddr(None)

0 comments on commit d714053

Please sign in to comment.