Browse Source

Fixed: Dependent on the "aiohttp" must be imported only in aiosocks.connector

main
nibrag 8 years ago
parent
commit
d7140532d4
6 changed files with 46 additions and 45 deletions
  1. +5
    -3
      README.rst
  2. +5
    -7
      aiosocks/__init__.py
  3. +17
    -4
      aiosocks/connector.py
  4. +1
    -10
      aiosocks/helpers.py
  5. +18
    -13
      tests/test_connector.py
  6. +0
    -8
      tests/test_helpers.py

+ 5
- 3
README.rst View File

@@ -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():
@@ -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:


+ 5
- 7
aiosocks/__init__.py View File

@@ -4,19 +4,17 @@ from .errors import (
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


+ 17
- 4
aiosocks/connector.py View File

@@ -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):


+ 1
- 10
aiosocks/helpers.py View File

@@ -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'])):
@@ -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)

+ 18
- 13
tests/test_connector.py View File

@@ -2,10 +2,9 @@ import unittest
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


@@ -133,18 +132,24 @@ class TestSocksConnector(unittest.TestCase):
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)

+ 0
- 8
tests/test_helpers.py View File

@@ -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)

||||||
x
 
000:0
Loading…
Cancel
Save