PySocks Documentation

repository·master·Indexed 23 days ago

https://github.com/anorov/pysocks

A SOCKS proxy client for Python that acts as a drop-in replacement for the standard socket module. It allows routing network traffic through SOCKS4, SOCKS5, and limited CONNECT-based HTTP proxy servers using the socks.socksocket class and set_proxy() method.

Tokens
1.4K
Snippets
6
Records
8
Agent score
29%

What's inside PySocks

  1. Monkeypatch the standard library to use a default proxy

    master

    You can force the entire Python standard library (and many third-party modules) to use a specific proxy by setting a default proxy and overriding the global socket.socket class.

    Warning: Monkeypatching is generally considered an anti-pattern in Python and may not work for all modules.

    import socket
    import socks
    
    # Set the default proxy for new socksockets
    socks.set_default_proxy(socks.SOCKS5, "localhost")
    
    # Override the standard socket class
    socket.socket = socks.socksocket
  2. Proxy HTTP traffic using requests

    master

    For proxying HTTP traffic, it is highly recommended to use the requests library. requests uses PySocks internally to handle SOCKS proxies. Avoid using PySocks' own HTTP proxy implementation as it only supports CONNECT-based proxies and is not recommended.

    import requests
    
    requests.get(url, proxies={
        "http": "socks5://proxyhostname:9050", 
        "https": "socks5://proxyhostname:9050"
    })
  3. Install PySocks

    master

    You can install PySocks using pip or by installing from source. This will install both the socks and sockshandler modules.

    Alternatively, you can simply include the socks.py file directly in your project.

    pip install pysocks
  4. Configure proxy settings with set_proxy()

    master

    The set_proxy method defines which proxy server the socksocket will use.

    Syntax: `set_proxy(proxy_type, addr[, port[, rdns[, username[, password]]]])

    Parameters:

    • proxy_type: One of socks.PROXY_TYPE_SOCKS4 (socks.SOCKS4), socks.PROXY_TYPE_SOCKS5 (socks.SOCKS5), or socks.PROXY_TYPE_HTTP (socks.HTTP).
    • addr: The IP address or DNS name of the proxy server.
    • port: The proxy port. Defaults to 1080 for SOCKS and 8080 for HTTP.
    • rdns: Boolean. If True (default), DNS resolution is performed remotely by the proxy. If False, resolution is performed locally. Note that for SOCKS4, this requires the SOCKS4a extension.
    • username: For SOCKS5, provides username authentication. For SOCKS4, this is sent as the userid. Ignored for HTTP.
    • password: For SOCKS5, provides the password for the username.
    s.set_proxy(socks.SOCKS5, "socks.example.com") # uses default port 1080
    s.set_proxy(socks.SOCKS4, "socks.test.com", 1081)
  5. Use socks.socksocket for SOCKS proxying

    master

    The socks.socksocket class provides an API identical to the standard library's socket.socket. You can configure a proxy for any socksocket instance using the set_proxy() method.

    Supported proxy types include:

    • socks.SOCKS5 (defaults to port 1080 if not specified)
    • socks.SOCKS4 (defaults to port 1080 if not specified)
    • socks.HTTP (supports CONNECT-based HTTP proxies)
    import socks
    
    s = socks.socksocket() # Same API as socket.socket in the standard lib
    
    # Configure SOCKS5 on localhost (default port 1080)
    s.set_proxy(socks.SOCKS5, "localhost") 
    
    # Or configure SOCKS4 on localhost with a specific port
    s.set_proxy(socks.SOCKS4, "localhost", 4444)
    
    # Or configure an HTTP proxy
    s.set_proxy(socks.HTTP, "5.5.5.5", 8888)
    
    # Use it like a regular socket
    s.connect(("www.somesite.com", 80))
    s.sendall("GET / HTTP/1.1 ...")
    print(s.recv(4096))
  6. Use socks.socksocket for proxy connections

    master

    The socks.socksocket class is the core of the module. It is derived from the standard socket class and can be used to create TCP connections through a SOCKS4, SOCKS5, or HTTP proxy.

    To use it, initialize the object (ideally without parameters), configure the proxy using set_proxy(), and then call connect() as you would with a normal socket.

  7. Handle proxy-related errors

    master

    All proxy-related exceptions in this module derive from socks.ProxyError, which itself descends from IOError. You can catch socks.ProxyError to handle any connection issue related to the proxy. Every ProxyError has a socket_err attribute containing the underlying socket.error (or None).

    Common Exception Classes:

    • socks.GeneralProxyError: For general issues like Sent invalid data, Connection closed unexpectedly, Bad proxy type, or Bad input.
    • socks.SOCKS5AuthError: For SOCKS5 authentication failures (e.g., Authentication is required, Unknown username or invalid password).
    • socks.SOCKS5Error: For SOCKS5 protocol errors (e.g., 0x01 General failure, 0x02 connection not allowed, 0x05 Connection refused).
    • socks.SOCKS4Error: For SOCKS4 protocol errors (e.g., 0x5B Request rejected, 0x5C identd failure).
    • socks.HTTPError: For HTTP proxy errors, containing the HTTP status code and message.