freshcrate
Home > Frameworks > python-socks

python-socks

Proxy (SOCKS4, SOCKS5, HTTP CONNECT) client for Python

Description

## python-socks [![CI](https://github.com/romis2012/python-socks/actions/workflows/ci.yml/badge.svg)](https://github.com/romis2012/python-socks/actions/workflows/ci.yml) [![Coverage Status](https://codecov.io/gh/romis2012/python-socks/branch/master/graph/badge.svg)](https://codecov.io/gh/romis2012/python-socks) [![PyPI version](https://badge.fury.io/py/python-socks.svg)](https://pypi.python.org/pypi/python-socks) [![versions](https://img.shields.io/pypi/pyversions/python-socks.svg)](https://github.com/romis2012/python-socks) <!-- [![Downloads](https://pepy.tech/badge/python-socks/month)](https://pepy.tech/project/python-socks) --> The `python-socks` package provides a core proxy client functionality for Python. Supports `SOCKS4(a)`, `SOCKS5(h)`, `HTTP CONNECT` proxy and provides sync and async (asyncio, trio, curio, anyio) APIs. You probably don't need to use `python-socks` directly. It is used internally by [aiohttp-socks](https://github.com/romis2012/aiohttp-socks) and [httpx-socks](https://github.com/romis2012/httpx-socks) packages. ## Requirements - Python >= 3.8 - async-timeout >= 4.0 (optional) - trio >= 0.24 (optional) - curio >= 1.4 (optional) - anyio >= 3.3.4 (optional) ## Installation only sync proxy support: ``` pip install python-socks ``` to include optional asyncio support: ``` pip install python-socks[asyncio] ``` to include optional trio support: ``` pip install python-socks[trio] ``` to include optional curio support: ``` pip install python-socks[curio] ``` to include optional anyio support: ``` pip install python-socks[anyio] ``` ## Simple usage We are making secure HTTP GET request via SOCKS5 proxy #### Sync ```python import ssl from python_socks.sync import Proxy proxy = Proxy.from_url('socks5://user:password@127.0.0.1:1080') # `connect` returns standard Python socket in blocking mode sock = proxy.connect(dest_host='check-host.net', dest_port=443) sock = ssl.create_default_context().wrap_socket( sock=sock, server_hostname='check-host.net' ) request = ( b'GET /ip HTTP/1.1\r\n' b'Host: check-host.net\r\n' b'Connection: close\r\n\r\n' ) sock.sendall(request) response = sock.recv(4096) print(response) ``` #### Async (asyncio) ```python import ssl import asyncio from python_socks.async_.asyncio import Proxy proxy = Proxy.from_url('socks5://user:password@127.0.0.1:1080') # `connect` returns standard Python socket in non-blocking mode # so we can pass it to asyncio.open_connection(...) sock = await proxy.connect(dest_host='check-host.net', dest_port=443) reader, writer = await asyncio.open_connection( host=None, port=None, sock=sock, ssl=ssl.create_default_context(), server_hostname='check-host.net', ) request = ( b'GET /ip HTTP/1.1\r\n' b'Host: check-host.net\r\n' b'Connection: close\r\n\r\n' ) writer.write(request) response = await reader.read(-1) print(response) ``` #### Async (trio) ```python import ssl import trio from python_socks.async_.trio import Proxy proxy = Proxy.from_url('socks5://user:password@127.0.0.1:1080') # `connect` returns trio socket # so we can pass it to trio.SocketStream sock = await proxy.connect(dest_host='check-host.net', dest_port=443) stream = trio.SocketStream(sock) stream = trio.SSLStream( stream, ssl.create_default_context(), server_hostname='check-host.net' ) await stream.do_handshake() request = ( b'GET /ip HTTP/1.1\r\n' b'Host: check-host.net\r\n' b'Connection: close\r\n\r\n' ) await stream.send_all(request) response = await stream.receive_some(4096) print(response) ``` #### Async (curio) ```python import curio.ssl as curiossl from python_socks.async_.curio import Proxy proxy = Proxy.from_url('socks5://user:password@127.0.0.1:1080') # `connect` returns curio.io.Socket sock = await proxy.connect( dest_host='check-host.net', dest_port=443 ) request = ( b'GET /ip HTTP/1.1\r\n' b'Host: check-host.net\r\n' b'Connection: close\r\n\r\n' ) ssl_context = curiossl.create_default_context() sock = await ssl_context.wrap_socket( sock, do_handshake_on_connect=False, server_hostname='check-host.net' ) await sock.do_handshake() stream = sock.as_stream() await stream.write(request) response = await stream.read(1024) print(response) ``` #### Async (anyio) ```python import ssl from python_socks.async_.anyio import Proxy proxy = Proxy.from_url('socks5://user:password@127.0.0.1:1080') # `connect` returns AnyioSocketStream stream = await proxy.connect( dest_host='check-host.net', dest_port=443, dest_ssl=ssl.create_default_context(), ) request = ( b'GET /ip HTTP/1.1\r\n' b'Host: check-host.net\r\n' b'Connection: close\r\n\r\n' ) await stream.write_all(request) response = await stream.read() print(response) ``` ## More complex example #### A urllib3 PoolManager that routes connections via the proxy ```python from urllib3 import PoolManager, HTTPConnectionPool, HTTPSConnectionPool from urllib3.connection import HTTPC

Release History

VersionChangesUrgencyDate
2.8.1Imported from PyPI (2.8.1)Low4/21/2026
v2.8.1Fix socks5 reply reading by @rigens (#46) Low2/16/2026
v2.8.0Add `ProxyException` base classLow12/9/2025
v2.7.3Release v2.7.3Low11/10/2025
v2.7.2Avoid leaking an open socket (trio) by @aaugustin (#44)Low8/1/2025
v2.7.1Close socket on loop.sock_connect(...) error by @aaugustin (#40)Low2/1/2025
v2.7.0Release v2.7.0Low1/28/2025
v2.6.1Release v2.6.1Low12/27/2024
v2.6.0Release v2.6.0Low12/26/2024
v2.5.3Release v2.5.3Low10/6/2024
v2.5.2Release v2.5.2Low9/25/2024
v2.5.1Release v2.5.1Low8/21/2024
v2.5.0Release v2.5.0Low6/25/2024
v2.4.4Release v2.4.4Low12/8/2023
v2.4.3Release v2.4.3Low9/26/2023
v2.4.2Release v2.4.2Low9/11/2023
v2.4.1Release v2.4.1Low9/8/2023
v2.4.0Release v2.4.0Low9/8/2023
v2.3.0Release v2.3.0Low5/8/2023
v2.2.0Release v2.2.0Low3/13/2023
v2.1.1Release v2.1.1Low12/19/2022
v2.1.0Release v2.1.0Low12/18/2022
v2.0.3Fix anyio exception handlingLow1/22/2022
v2.0.2Fixed #14 anyio.BrokenResourceError has no attribute 'strerror'Low1/13/2022
v2.0.1Release v2.0.1Low12/17/2021
v2.0.0- Added anyio backend - Added new (v2) API for sync and trio backends - The code base has been completely redesignedLow11/23/2021
v1.2.4Release v1.2.4Low3/30/2021
v1.2.3Release v1.2.3Low3/23/2021
v1.2.2Release v1.2.2Low3/1/2021
v1.2.1Release v1.2.1Low2/16/2021
v1.2.0Release v1.2.0Low1/17/2021
v1.1.3Release v1.1.3Low1/16/2021
v1.1.2Release v1.1.2Low12/16/2020
v1.1.1Release v1.1.1Low11/22/2020
v1.1.0Add curio backend supportLow9/21/2020
v1.0.1Release v1.0.1Low9/20/2020

Dependencies & License Audit

Loading dependencies...

Similar Packages

greenbackReenter an async event loop from synchronous code1.3.0
sniffioSniff out which async library your code is running under1.3.1
txaioCompatibility API between asyncio/Twisted/Trollius25.12.2
autobahnWebSocket client & server library, WAMP real-time framework25.12.2
aiolimiterasyncio rate limiter, a leaky bucket implementation1.2.1