Browse Source

Fixed: python 3.4 compability

main
nibrag 8 years ago
parent
commit
bf0abf911a
2 changed files with 17 additions and 7 deletions
  1. +6
    -1
      aiosocks/protocols.py
  2. +11
    -6
      tests/test_protocol.py

+ 6
- 1
aiosocks/protocols.py View File

@@ -10,6 +10,11 @@ from .errors import (
InvalidServerReply, InvalidServerVersion
)

try:
from asyncio import ensure_future
except ImportError:
ensure_future = asyncio.async


class BaseSocksProtocol(asyncio.StreamReaderProtocol):
def __init__(self, proxy, proxy_auth, dst, remote_resolve=True, loop=None):
@@ -36,7 +41,7 @@ class BaseSocksProtocol(asyncio.StreamReaderProtocol):
self._transport = transport

req_coro = self.socks_request(c.SOCKS_CMD_CONNECT)
self._negotiate_done = asyncio.ensure_future(req_coro, loop=self._loop)
self._negotiate_done = ensure_future(req_coro, loop=self._loop)

@asyncio.coroutine
def socks_request(self, cmd):


+ 11
- 6
tests/test_protocol.py View File

@@ -7,6 +7,11 @@ from asyncio import coroutine as coro
import aiosocks.constants as c
from aiosocks.protocols import BaseSocksProtocol

try:
from asyncio import ensure_future
except ImportError:
ensure_future = asyncio.async


def make_socks4(loop, *, addr=None, auth=None, rr=True, dst=None, r=b''):
addr = addr or aiosocks.Socks4Addr('localhost', 1080)
@@ -179,7 +184,7 @@ class TestSocks4Protocol(unittest.TestCase):

# valid result
proto = make_socks4(self.loop, r=valid_resp)
req = asyncio.ensure_future(
req = ensure_future(
proto.socks_request(c.SOCKS_CMD_CONNECT), loop=self.loop)
self.loop.run_until_complete(req)

@@ -323,7 +328,7 @@ class TestSocks5Protocol(unittest.TestCase):
# ipv4
proto = make_socks5(
self.loop, r=[b'\x01', b'\x7f\x00\x00\x01', b'\x00P'])
req = asyncio.ensure_future(proto.read_address(), loop=self.loop)
req = ensure_future(proto.read_address(), loop=self.loop)
self.loop.run_until_complete(req)

self.assertEqual(req.result(), ('127.0.0.1', 80))
@@ -335,7 +340,7 @@ class TestSocks5Protocol(unittest.TestCase):
b'\x00P'
]
proto = make_socks5(self.loop, r=resp)
req = asyncio.ensure_future(proto.read_address(), loop=self.loop)
req = ensure_future(proto.read_address(), loop=self.loop)
self.loop.run_until_complete(req)

self.assertEqual(
@@ -344,7 +349,7 @@ class TestSocks5Protocol(unittest.TestCase):
# domain
proto = make_socks5(
self.loop, r=[b'\x03', b'\n', b'python.org', b'\x00P'])
req = asyncio.ensure_future(proto.read_address(), loop=self.loop)
req = ensure_future(proto.read_address(), loop=self.loop)
self.loop.run_until_complete(req)

self.assertEqual(req.result(), (b'python.org', 80))
@@ -379,8 +384,8 @@ class TestSocks5Protocol(unittest.TestCase):
b'\x01', b'\x7f\x00\x00\x01',
b'\x00P']
proto = make_socks5(self.loop, r=resp)
req = asyncio.ensure_future(proto.socks_request(c.SOCKS_CMD_CONNECT),
loop=self.loop)
req = ensure_future(proto.socks_request(c.SOCKS_CMD_CONNECT),
loop=self.loop)
self.loop.run_until_complete(req)

self.assertEqual(req.result(), (('python.org', 80), ('127.0.0.1', 80)))


Loading…
Cancel
Save