Python3: asyncio + SOCKS5 Client support

Python3: asyncio + SOCKS5 Client support

Postby flugplatz89 » Thu May 29, 2014 2:27 pm

Hello people.
I am interested in using asyncio in Python3, however, I have not found a way to make it work through a socks proxy forwarding my requests to another host. After a bit of research I have found the pysocks library, which, after fixing it to work with Python3, successfully sends bytes synchronously through a socks proxy.

My problem, however, lies in making it work with asyncio. After combing through the code for a while I have found out where the actual connection is established:
Code: Select all
base_events.py, 395-397:
                try:
                    sock = socket.socket(family=family, type=type, proto=proto)
                    sock.setblocking(False)
[...]


I decided to insert the socks socket, changing the code to this:
Code: Select all
                try:
                    #sock = socket.socket(family=family, type=type, proto=proto)
                    from . import socks
                    sock = socks.socksocket()
                    sock.setproxy(socks.PROXY_TYPE_SOCKS5, "localhost", 1234)

                    sock.setblocking(False)
[...]


Executing this leaves me with the following backtrace:
Code: Select all
future: Task(<create_connection>)<exception=BlockingIOError(115, 'Operation now in progress')>
Traceback (most recent call last):
  File "/home/flugplatz89/python/aiohttp/asyncio/tasks.py", line 300, in _step
    result = coro.send(value)
  File "/home/flugplatz89/python/aiohttp/asyncio/base_events.py", line 425, in create_connection
    raise exceptions[0]
  File "/home/flugplatz89/python/aiohttp/asyncio/base_events.py", line 416, in create_connection
    yield from self.sock_connect(sock, address)
  File "/home/flugplatz89/python/aiohttp/asyncio/futures.py", line 350, in __iter__   
    return self.result()  # May raise too.
  File "/home/flugplatz89/python/aiohttp/asyncio/futures.py", line 243, in result
    raise self._exception
  File "/home/flugplatz89/python/aiohttp/asyncio/selector_events.py", line 297, in _sock_connect
    sock.connect(address)
BlockingIOError: [Errno 115] Operation now in progress


For some reason, the connection process is being interrupted. How can I make it work with asyncio? Is there possibly a cleaner way?

Thanks for your help and consideration.
Last edited by stranac on Thu May 29, 2014 3:03 pm, edited 1 time in total.
Reason: First post lock.
flugplatz89
 
Posts: 1
Joined: Wed May 28, 2014 7:33 pm

Return to Networking

Who is online

Users browsing this forum: W3C [Linkcheck] and 1 guest