1SocksiPy version 1.00
2A Python SOCKS module.
3(C) 2006 Dan-Haim. All rights reserved.
4See LICENSE file for details.
5
6
7WHAT IS A SOCKS PROXY?
8A SOCKS proxy is a proxy server at the TCP level. In other words, it acts as
9a tunnel, relaying all traffic going through it without modifying it.
10SOCKS proxies can be used to relay traffic using any network protocol that
11uses TCP.
12
13WHAT IS SOCKSIPY?
14This Python module allows you to create TCP connections through a SOCKS
15proxy without any special effort.
16
17PROXY COMPATIBILITY
18SocksiPy is compatible with three different types of proxies:
191. SOCKS Version 4 (Socks4), including the Socks4a extension.
202. SOCKS Version 5 (Socks5).
213. HTTP Proxies which support tunneling using the CONNECT method.
22
23SYSTEM REQUIREMENTS
24Being written in Python, SocksiPy can run on any platform that has a Python
25interpreter and TCP/IP support.
26This module has been tested with Python 2.3 and should work with greater versions
27just as well.
28
29
30INSTALLATION
31-------------
32
33Simply copy the file "socks.py" to your Python's lib/site-packages directory,
34and you're ready to go.
35
36
37USAGE
38------
39
40First load the socks module with the command:
41
42>>> import socks
43>>>
44
45The socks module provides a class called "socksocket", which is the base to
46all of the module's functionality.
47The socksocket object has the same initialization parameters as the normal socket
48object to ensure maximal compatibility, however it should be noted that socksocket
49will only function with family being AF_INET and type being SOCK_STREAM.
50Generally, it is best to initialize the socksocket object with no parameters
51
52>>> s = socks.socksocket()
53>>>
54
55The socksocket object has an interface which is very similiar to socket's (in fact
56the socksocket class is derived from socket) with a few extra methods.
57To select the proxy server you would like to use, use the setproxy method, whose
58syntax is:
59
60setproxy(proxytype, addr[, port[, rdns[, username[, password]]]])
61
62Explaination of the parameters:
63
64proxytype - The type of the proxy server. This can be one of three possible
65choices: PROXY_TYPE_SOCKS4, PROXY_TYPE_SOCKS5 and PROXY_TYPE_HTTP for Socks4,
66Socks5 and HTTP servers respectively.
67
68addr - The IP address or DNS name of the proxy server.
69
70port - The port of the proxy server. Defaults to 1080 for socks and 8080 for http.
71
72rdns - This is a boolean flag than modifies the behavior regarding DNS resolving.
73If it is set to True, DNS resolving will be preformed remotely, on the server.
74If it is set to False, DNS resolving will be preformed locally. Please note that
75setting this to True with Socks4 servers actually use an extension to the protocol,
76called Socks4a, which may not be supported on all servers (Socks5 and http servers
77always support DNS). The default is True.
78
79username - For Socks5 servers, this allows simple username / password authentication
80with the server. For Socks4 servers, this parameter will be sent as the userid.
81This parameter is ignored if an HTTP server is being used. If it is not provided,
82authentication will not be used (servers may accept unauthentication requests).
83
84password - This parameter is valid only for Socks5 servers and specifies the
85respective password for the username provided.
86
87Example of usage:
88
89>>> s.setproxy(socks.PROXY_TYPE_SOCKS5,"socks.example.com")
90>>>
91
92After the setproxy method has been called, simply call the connect method with the
93traditional parameters to establish a connection through the proxy:
94
95>>> s.connect(("www.sourceforge.net",80))
96>>>
97
98Connection will take a bit longer to allow negotiation with the proxy server.
99Please note that calling connect without calling setproxy earlier will connect
100without a proxy (just like a regular socket).
101
102Errors: Any errors in the connection process will trigger exceptions. The exception
103may either be generated by the underlying socket layer or may be custom module
104exceptions, whose details follow:
105
106class ProxyError - This is a base exception class. It is not raised directly but
107rather all other exception classes raised by this module are derived from it.
108This allows an easy way to catch all proxy-related errors.
109
110class GeneralProxyError - When thrown, it indicates a problem which does not fall
111into another category. The parameter is a tuple containing an error code and a
112description of the error, from the following list:
1131 - invalid data - This error means that unexpected data has been received from
114the server. The most common reason is that the server specified as the proxy is
115not really a Socks4/Socks5/HTTP proxy, or maybe the proxy type specified is wrong.
1164 - bad proxy type - This will be raised if the type of the proxy supplied to the
117setproxy function was not PROXY_TYPE_SOCKS4/PROXY_TYPE_SOCKS5/PROXY_TYPE_HTTP.
1185 - bad input - This will be raised if the connect method is called with bad input
119parameters.
120
121class Socks5AuthError - This indicates that the connection through a Socks5 server
122failed due to an authentication problem. The parameter is a tuple containing a
123code and a description message according to the following list:
124
1251 - authentication is required - This will happen if you use a Socks5 server which
126requires authentication without providing a username / password at all.
1272 - all offered authentication methods were rejected - This will happen if the proxy
128requires a special authentication method which is not supported by this module.
1293 - unknown username or invalid password - Self descriptive.
130
131class Socks5Error - This will be raised for Socks5 errors which are not related to
132authentication. The parameter is a tuple containing a code and a description of the
133error, as given by the server. The possible errors, according to the RFC are:
134
1351 - General SOCKS server failure - If for any reason the proxy server is unable to
136fulfill your request (internal server error).
1372 - connection not allowed by ruleset - If the address you're trying to connect to
138is blacklisted on the server or requires authentication.
1393 - Network unreachable - The target could not be contacted. A router on the network
140had replied with a destination net unreachable error.
1414 - Host unreachable - The target could not be contacted. A router on the network
142had replied with a destination host unreachable error.
1435 - Connection refused - The target server has actively refused the connection
144(the requested port is closed).
1456 - TTL expired - The TTL value of the SYN packet from the proxy to the target server
146has expired. This usually means that there are network problems causing the packet
147to be caught in a router-to-router "ping-pong".
1487 - Command not supported - The client has issued an invalid command. When using this
149module, this error should not occur.
1508 - Address type not supported - The client has provided an invalid address type.
151When using this module, this error should not occur.
152
153class Socks4Error - This will be raised for Socks4 errors. The parameter is a tuple
154containing a code and a description of the error, as given by the server. The
155possible error, according to the specification are:
156
1571 - Request rejected or failed - Will be raised in the event of an failure for any
158reason other then the two mentioned next.
1592 - request rejected because SOCKS server cannot connect to identd on the client -
160The Socks server had tried an ident lookup on your computer and has failed. In this
161case you should run an identd server and/or configure your firewall to allow incoming
162connections to local port 113 from the remote server.
1633 - request rejected because the client program and identd report different user-ids - 
164The Socks server had performed an ident lookup on your computer and has received a
165different userid than the one you have provided. Change your userid (through the
166username parameter of the setproxy method) to match and try again.
167
168class HTTPError - This will be raised for HTTP errors. The parameter is a tuple
169containing the HTTP status code and the description of the server.
170
171
172After establishing the connection, the object behaves like a standard socket.
173Call the close method to close the connection.
174
175In addition to the socksocket class, an additional function worth mentioning is the
176setdefaultproxy function. The parameters are the same as the setproxy method.
177This function will set default proxy settings for newly created socksocket objects,
178in which the proxy settings haven't been changed via the setproxy method.
179This is quite useful if you wish to force 3rd party modules to use a socks proxy,
180by overriding the socket object.
181For example:
182
183>>> socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5,"socks.example.com")
184>>> socket.socket = socks.socksocket
185>>> urllib.urlopen("http://www.sourceforge.net/")
186
187
188PROBLEMS
189---------
190
191If you have any problems using this module, please first refer to the BUGS file
192(containing current bugs and issues). If your problem is not mentioned you may
193contact the author at the following E-Mail address:
194
195negativeiq@users.sourceforge.net
196
197Please allow some time for your question to be received and handled.
198
199
200Dan-Haim,
201Author.
202