Typing stubs for PySocks
types-pysocks provides static type checking stubs for the PySocks library, enabling developers to write more robust and type-safe Python code when working with SOCKS proxy servers. PySocks itself is a Python module that offers a straightforward interface for routing network traffic through SOCKS proxies. The types-pysocks package, maintained by the Python community's typeshed project, keeps its stubs updated to reflect the PySocks API, with version 1.7.1.20260408 currently supporting Python 3.10 and newer. While the underlying PySocks library's direct development cadence might vary, the types-pysocks stubs are actively maintained.
Common errors
-
GeneralProxyError: Connection closed unexpectedly
cause The proxy server closed the connection prematurely, possibly due to network issues, software problems on the proxy, or invalid negotiation.fixVerify the proxy server's status, network connectivity, and ensure the proxy type (SOCKS4, SOCKS5, HTTP) is correctly specified. Check proxy server logs for more details. -
GeneralProxyError: Bad proxy type
cause The `proxy_type` parameter provided to `set_proxy` or `set_default_proxy` was not one of the valid `socks.SOCKS4`, `socks.SOCKS5`, or `socks.HTTP` constants.fixEnsure you are using `socks.SOCKS4`, `socks.SOCKS5`, or `socks.HTTP` for the `proxy_type` argument. Double-check your proxy server's actual type. -
SOCKS5AuthError: Authentication is required
cause You attempted to connect to a SOCKS5 proxy server that requires authentication, but no username and password were provided in `set_proxy` or `set_default_proxy`.fixProvide a valid `username` and `password` to the `set_proxy` or `set_default_proxy` function when connecting to an authenticated SOCKS5 proxy. Example: `socks.set_default_proxy(socks.SOCKS5, 'host', 9050, True, 'your_user', 'your_pass')`. -
GeneralProxyError: Sent invalid data
cause The proxy server received unexpected or malformed data, indicating a potential mismatch in protocol or a non-compliant proxy.fixConfirm that the proxy server is indeed a SOCKS (or CONNECT-based HTTP) proxy and that its configuration matches the `proxy_type` you've specified. This can also happen if the proxy server is misconfigured or overloaded.
Warnings
- gotcha PySocks' `socks.set_default_proxy` globally monkey-patches `socket.socket`. While convenient, this can have unintended side effects on other parts of your application or libraries that also interact with the socket module directly. Consider using `requests` with its `proxies` argument for HTTP/HTTPS traffic, which uses PySocks internally in a less intrusive way, or manually creating `socks.socksocket` instances for fine-grained control.
- gotcha PySocks' native HTTP proxy support is limited to proxies that use CONNECT tunneling. For standard HTTP proxies, it is generally recommended to use the HTTP client's native proxy support (e.g., `requests`' `proxies` parameter or `urllib.request.ProxyHandler`).
- gotcha When using `requests` with a SOCKS proxy, you might encounter issues with DNS resolution if the proxy URL uses `socks5://`. By default, `requests` may attempt local DNS resolution.
- gotcha PySocks has limited support for IPv6. Attempting to use IPv6 addresses might lead to errors or unexpected behavior.
Install
-
pip install pysocks types-pysocks
Imports
- socks
from types_pysocks import socks
import socks
- set_default_proxy
import socks socks.set_default_proxy(...)
Quickstart
import socks
import socket
import urllib.request
import os
# --- Configuration for SOCKS proxy ---
# Replace with your proxy details
PROXY_TYPE = socks.SOCKS5 # or socks.SOCKS4, socks.HTTP
PROXY_ADDR = os.environ.get('SOCKS_PROXY_ADDR', '127.0.0.1')
PROXY_PORT = int(os.environ.get('SOCKS_PROXY_PORT', 9050))
PROXY_USERNAME = os.environ.get('SOCKS_PROXY_USER', '')
PROXY_PASSWORD = os.environ.get('SOCKS_PROXY_PASS', '')
# Set up the default proxy for all subsequent socket connections
if PROXY_USERNAME and PROXY_PASSWORD:
socks.set_default_proxy(PROXY_TYPE, PROXY_ADDR, PROXY_PORT, True, PROXY_USERNAME, PROXY_PASSWORD)
else:
socks.set_default_proxy(PROXY_TYPE, PROXY_ADDR, PROXY_PORT)
# "Monkey-patch" the socket module to use PySocks
socket.socket = socks.socksocket
try:
# Make a request that will now go through the SOCKS proxy
print(f"Attempting to fetch external IP via proxy {PROXY_ADDR}:{PROXY_PORT}...")
with urllib.request.urlopen('http://icanhazip.com') as response:
external_ip = response.read().decode('utf-8').strip()
print(f"External IP (via proxy): {external_ip}")
except socks.ProxyError as e:
print(f"Failed to connect via proxy: {e}")
except urllib.error.URLError as e:
print(f"URL Error: {e.reason}")
except Exception as e:
print(f"An unexpected error occurred: {e}")