Home

Awesome

PySocks

PySocks lets you send traffic through SOCKS proxy servers. It is a modern fork of SocksiPy with bug fixes and extra features.

Acts as a drop-in replacement to the socket module. Seamlessly configure SOCKS proxies for any socket object by calling socket_object.set_proxy().


Features

Installation

pip install pysocks

Or download the tarball / git clone and...

python setup.py install

These will install both the socks and sockshandler modules.

Alternatively, include just socks.py in your project.

Proxying HTTP Traffic

We highly recommend using the requests library for proxying HTTP traffic with SOCKS or HTTP proxies. It uses PySocks under the hood.

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

PySocks has an option for HTTP proxies, but it only supports CONNECT-based HTTP proxies, and in general we recommend using your HTTP client's native proxy support (such as requests' proxies keyword argument) rather than PySocks'.

If you absolutely must, you can use the urllib2 handler in sockshandler.py, but it's not supported (and won't work for non-CONNECT-based HTTP proxies, as stated above).


Usage

socks.socksocket

import socks

s = socks.socksocket() # Same API as socket.socket in the standard lib

s.set_proxy(socks.SOCKS5, "localhost") # SOCKS4 and SOCKS5 use port 1080 by default
# Or
s.set_proxy(socks.SOCKS4, "localhost", 4444)
# Or
s.set_proxy(socks.HTTP, "5.5.5.5", 8888)

# Can be treated like a regular socket object
s.connect(("www.somesite.com", 80))
s.sendall("GET / HTTP/1.1 ...")
print(s.recv(4096))

Monkeypatching

To monkeypatch the entire standard library with a single default proxy:

import urllib2
import socket
import socks

socks.set_default_proxy(socks.SOCKS5, "localhost")
socket.socket = socks.socksocket

Note that monkeypatching may not work for all standard modules or for all third party modules, and generally isn't recommended. Monkeypatching is usually an anti-pattern in Python.


Original SocksiPy README attached below, amended to reflect API changes.


SocksiPy

A Python SOCKS module.

(C) 2006 Dan-Haim. All rights reserved.

See LICENSE file for details.

WHAT IS A SOCKS PROXY?

A SOCKS proxy is a proxy server at the TCP level. In other words, it acts as a tunnel, relaying all traffic going through it without modifying it. SOCKS proxies can be used to relay traffic using any network protocol that uses TCP.

WHAT IS SOCKSIPY?

This Python module allows you to create TCP connections through a SOCKS proxy without any special effort. It also supports relaying UDP packets with a SOCKS5 proxy.

PROXY COMPATIBILITY

SocksiPy is compatible with three different types of proxies:

  1. SOCKS Version 4 (SOCKS4), including the SOCKS4a extension.
  2. SOCKS Version 5 (SOCKS5).
  3. HTTP Proxies which support tunneling using the CONNECT method.

SYSTEM REQUIREMENTS

Being written in Python, SocksiPy can run on any platform that has a Python interpreter and TCP/IP support. This module has been tested with Python 2.3 and should work with greater versions just as well.

INSTALLATION

Simply copy the file "socks.py" to your Python's lib/site-packages directory, and you're ready to go. [Editor's note: it is better to use python setup.py install for PySocks]

USAGE

First load the socks module with the command:

>>> import socks
>>>

The socks module provides a class called socksocket, which is the base to all of the module's functionality.

The socksocket object has the same initialization parameters as the normal socket object to ensure maximal compatibility, however it should be noted that socksocket will only function with family being AF_INET and type being either SOCK_STREAM or SOCK_DGRAM. Generally, it is best to initialize the socksocket object with no parameters

>>> s = socks.socksocket()
>>>

The socksocket object has an interface which is very similiar to socket's (in fact the socksocket class is derived from socket) with a few extra methods. To select the proxy server you would like to use, use the set_proxy method, whose syntax is:

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

Explanation of the parameters:

proxy_type - The type of the proxy server. This can be one of three possible choices: PROXY_TYPE_SOCKS4, PROXY_TYPE_SOCKS5 and PROXY_TYPE_HTTP for SOCKS4, SOCKS5 and HTTP servers respectively. SOCKS4, SOCKS5, and HTTP are all aliases, respectively.

addr - The IP address or DNS name of the proxy server.

port - The port of the proxy server. Defaults to 1080 for socks and 8080 for http.

rdns - This is a boolean flag than modifies the behavior regarding DNS resolving. If it is set to True, DNS resolving will be preformed remotely, on the server. If it is set to False, DNS resolving will be preformed locally. Please note that setting this to True with SOCKS4 servers actually use an extension to the protocol, called SOCKS4a, which may not be supported on all servers (SOCKS5 and http servers always support DNS). The default is True.

username - For SOCKS5 servers, this allows simple username / password authentication with the server. For SOCKS4 servers, this parameter will be sent as the userid. This parameter is ignored if an HTTP server is being used. If it is not provided, authentication will not be used (servers may accept unauthenticated requests).

password - This parameter is valid only for SOCKS5 servers and specifies the respective password for the username provided.

Example of usage:

>>> s.set_proxy(socks.SOCKS5, "socks.example.com") # uses default port 1080
>>> s.set_proxy(socks.SOCKS4, "socks.test.com", 1081)

After the set_proxy method has been called, simply call the connect method with the traditional parameters to establish a connection through the proxy:

>>> s.connect(("www.sourceforge.net", 80))
>>>

Connection will take a bit longer to allow negotiation with the proxy server. Please note that calling connect without calling set_proxy earlier will connect without a proxy (just like a regular socket).

Errors: Any errors in the connection process will trigger exceptions. The exception may either be generated by the underlying socket layer or may be custom module exceptions, whose details follow:

class ProxyError - This is a base exception class. It is not raised directly but rather all other exception classes raised by this module are derived from it. This allows an easy way to catch all proxy-related errors. It descends from IOError.

All ProxyError exceptions have an attribute socket_err, which will contain either a caught socket.error exception, or None if there wasn't any.

class GeneralProxyError - When thrown, it indicates a problem which does not fall into another category.

class SOCKS5AuthError - This indicates that the connection through a SOCKS5 server failed due to an authentication problem.

class SOCKS5Error - This will be raised for SOCKS5 errors which are not related to authentication. The parameter is a tuple containing a code, as given by the server, and a description of the error. The possible errors, according to the RFC, are:

class SOCKS4Error - This will be raised for SOCKS4 errors. The parameter is a tuple containing a code and a description of the error, as given by the server. The possible error, according to the specification are:

class HTTPError - This will be raised for HTTP errors. The message will contain the HTTP status code and provided error message.

After establishing the connection, the object behaves like a standard socket.

Methods like makefile() and settimeout() should behave just like regular sockets. Call the close() method to close the connection.

In addition to the socksocket class, an additional function worth mentioning is the set_default_proxy function. The parameters are the same as the set_proxy method. This function will set default proxy settings for newly created socksocket objects, in which the proxy settings haven't been changed via the set_proxy method. This is quite useful if you wish to force 3rd party modules to use a SOCKS proxy, by overriding the socket object. For example:

>>> socks.set_default_proxy(socks.SOCKS5, "socks.example.com")
>>> socket.socket = socks.socksocket
>>> urllib.urlopen("http://www.sourceforge.net/")

PROBLEMS

Please open a GitHub issue at https://github.com/Anorov/PySocks