Core proxy (SOCKS4, SOCKS5, HTTP tunneling) functionality for Python
Project description
python-socks
The python-socks package provides a core proxy client functionality for Python. Supports SOCKS4(a), SOCKS5, HTTP (tunneling) proxy and provides sync and async (asyncio, trio) APIs.
Requirements
- Python >= 3.6
- async-timeout >= 3.0.1 (optional)
- trio >= 0.16.0 (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]
Usage
We are making secure HTTP GET request via SOCKS5 proxy
Sync
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)
import ssl
import asyncio
from python_socks.sync 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)
request = (
b'GET /ip HTTP/1.1\r\n'
b'Host: check-host.net\r\n'
b'Connection: close\r\n\r\n'
)
reader, writer = await asyncio.open_connection(
host=None,
port=None,
sock=sock,
ssl=ssl.create_default_context(),
server_hostname='check-host.net',
)
writer.write(request)
response = await reader.read(-1)
print(response)
Async (trio)
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)
request = (
b'GET /ip HTTP/1.1\r\n'
b'Host: check-host.net\r\n'
b'Connection: close\r\n\r\n'
)
stream = trio.SocketStream(sock)
stream = trio.SSLStream(
stream, ssl.create_default_context(),
server_hostname='check-host.net'
)
await stream.do_handshake()
await stream.send_all(request)
response = await stream.receive_some(4096)
print(response)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file python-socks-1.0.0.tar.gz.
File metadata
- Download URL: python-socks-1.0.0.tar.gz
- Upload date:
- Size: 15.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/44.0.0.post20200106 requests-toolbelt/0.9.1 tqdm/4.42.1 CPython/3.7.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4ebc87e00350b29e4742528669f5ff49afa0780676b15623ac2b08ac6999b283
|
|
| MD5 |
999ed83a10a72f962a930e37c604a7ed
|
|
| BLAKE2b-256 |
b4cb64398c0bff915fc53e66365c6f977acbf789b5854f5fb54dec81c38a5c61
|
File details
Details for the file python_socks-1.0.0-py3-none-any.whl.
File metadata
- Download URL: python_socks-1.0.0-py3-none-any.whl
- Upload date:
- Size: 28.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/44.0.0.post20200106 requests-toolbelt/0.9.1 tqdm/4.42.1 CPython/3.7.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4a5dffb772b338869b149983c063bb114b3013e9921131dad78c9ec43c3f553d
|
|
| MD5 |
3c72db7ab8e9c302dae7e027cb608b16
|
|
| BLAKE2b-256 |
c1115eefc3f343206c274cbdf9eeddf68da301a001b8f56edfeb784c7e61895c
|