Last modified: Jun 01, 2025 By Alexander Williams
Install PySocks for Python Proxy Support
PySocks is a Python library that simplifies proxy support. It allows you to route your network traffic through SOCKS or HTTP proxies. This guide covers installation and usage.
What is PySocks?
PySocks is a fork of SocksiPy. It provides proxy support for Python applications. It works with SOCKS4, SOCKS5, and HTTP proxies.
The library is compatible with Python's socket
module. It seamlessly integrates with existing code.
Install PySocks via pip
Installing PySocks is simple using pip. Run this command in your terminal:
pip install PySocks
For more complex Python setups, you might need to check our guide on Install Pytest-mock for Python Mocking Tests.
Basic PySocks Usage
After installation, import the library in your Python script:
import socks
import socket
Setting Up a Proxy Connection
Here's how to configure a SOCKS5 proxy:
# Set the default socket to use the proxy
socks.set_default_proxy(socks.SOCKS5, "proxy.example.com", 1080)
socket.socket = socks.socksocket
# Now all connections will go through the proxy
try:
print(socket.gethostbyname("google.com"))
except Exception as e:
print(f"Error: {e}")
The output should show Google's IP address routed through your proxy.
Advanced Proxy Configuration
PySocks supports authentication and different proxy types:
# With authentication
socks.set_default_proxy(
socks.SOCKS5,
"proxy.example.com",
1080,
username="user",
password="pass"
)
# For HTTP proxies
socks.set_default_proxy(
socks.HTTP,
"http-proxy.example.com",
8080
)
Using PySocks with Requests Library
You can combine PySocks with the popular requests
library:
import requests
import socks
import socket
socks.set_default_proxy(socks.SOCKS5, "localhost", 9050)
socket.socket = socks.socksocket
response = requests.get("https://example.com")
print(response.status_code)
For other networking tasks, consider learning about Pexpect for Python Automation.
Error Handling
Always handle potential proxy connection errors:
try:
socks.set_default_proxy(socks.SOCKS5, "invalid.proxy", 1080)
socket.socket = socks.socksocket
print(socket.gethostbyname("example.com"))
except socks.ProxyConnectionError as e:
print(f"Proxy connection failed: {e}")
except socks.GeneralProxyError as e:
print(f"General proxy error: {e}")
PySocks vs Other Proxy Solutions
PySocks offers several advantages:
Lightweight: It's a small library with minimal dependencies.
Compatible: Works with most Python networking libraries.
Flexible: Supports multiple proxy protocols.
For documentation needs, check our guide on Installing Sphinx for Python Documentation.
Conclusion
PySocks is an essential tool for Python developers needing proxy support. It's easy to install and integrates well with existing code.
The library supports various proxy types and authentication methods. Always remember to handle connection errors properly.
With PySocks, you can easily route your Python application traffic through proxies. This is useful for web scraping, privacy, and testing.