asysocks

raw JSON →
0.2.18 verified Fri May 01 auth: no python

Asynchronous SOCKS4/5, HTTP proxy and SSH tunnel client library for Python, built on the asyncio framework. Current version 0.2.18, with irregular releases focusing on bug fixes and minor feature additions.

pip install asysocks
error ModuleNotFoundError: No module named 'asysocks'
cause Library not installed or installed in wrong Python environment.
fix
Run 'pip install asysocks' and ensure you are using the correct Python interpreter.
error AttributeError: module 'asysocks' has no attribute 'SocksClient'
cause Trying to import SocksClient directly from the package instead of the submodule.
fix
Use 'from asysocks.client import SocksClient'.
error RuntimeError: Task <Task pending> got Future <Future pending> attached to a different loop
cause Creating the client in one event loop and using it in another, or mixing asyncio and threading.
fix
Ensure all asyncio operations run in the same event loop. Use asyncio.run() for top-level.
gotcha The library uses asyncio, not threading. Do not call connect/send/recv from different threads without proper synchronization.
fix Use asyncio.run_coroutine_threadsafe or run all calls in the same event loop.
deprecated Old import path 'asysocks.SocksClient' is removed in 0.2.0+. Use 'from asysocks.client import SocksClient'.
fix Update import to from asysocks.client import SocksClient.
breaking In version 0.2.0, the API was rewritten. The previous synchronous client no longer exists. All methods return coroutines.
fix Use async/await patterns. Refer to the quickstart example.

Connect to example.com via a SOCKS5 proxy at localhost:1080 and send an HTTP request.

import asyncio
from asysocks.client import SocksClient

async def main():
    client = SocksClient(
        target=('example.com', 80),
        proxy=('127.0.0.1', 1080),
        proxy_type='socks5'
    )
    await client.connect()
    await client.send(b'GET / HTTP/1.0\r\n\r\n')
    response = await client.recv()
    print(response)
    await client.close()

asyncio.run(main())