PySocks

raw JSON →
1.7.1 verified Sat May 09 auth: no python

PySocks is a core SOCKS client module that provides a socket wrapper supporting SOCKS4, SOCKS5, and HTTP proxy tunneling. The current version is 1.7.1. Releases are infrequent and focus on maintenance.

pip install PySocks
error ImportError: No module named socks
cause The stub 'socks' package (version 0) was installed instead of PySocks, or socks is not installed.
fix
pip install PySocks
error AttributeError: module 'socks' has no attribute 'SOCKS5'
cause The stub 'socks' package (version 0) does not contain the real modules.
fix
pip uninstall socks && pip install PySocks
gotcha Monkey-patching socket.socket affects entire Python process; avoid in production or use per-connection approach.
fix Use socks.socksocket directly for individual sockets instead of setting socket.socket
deprecated The original 'socks' package on PyPI (version 0) is a stub that does nothing; use 'PySocks' instead.
fix pip install PySocks and import socks; uninstall the stub package: pip uninstall socks
gotcha PySocks does not support authentication for SOCKS4; only SOCKS5 supports username/password.
fix Use SOCKS5 proxy type if authentication is needed.

Demonstrates monkey-patching socket to route all traffic through a SOCKS5 proxy.

import socks
import socket

socks.setdefaultproxy(socks.SOCKS5, 'localhost', 9050)
socket.socket = socks.socksocket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('httpbin.org', 80))
s.sendall(b'GET /ip HTTP/1.1\r\nHost: httpbin.org\r\n\r\n')
print(s.recv(4096).decode())
s.close()