METADATA 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. Metadata-Version: 2.1
  2. Name: PySocks
  3. Version: 1.7.1
  4. Summary: A Python SOCKS client module. See https://github.com/Anorov/PySocks for more information.
  5. Home-page: https://github.com/Anorov/PySocks
  6. Author: Anorov
  7. Author-email: anorov.vorona@gmail.com
  8. License: BSD
  9. Keywords: socks,proxy
  10. Platform: UNKNOWN
  11. Classifier: Programming Language :: Python :: 2
  12. Classifier: Programming Language :: Python :: 2.7
  13. Classifier: Programming Language :: Python :: 3
  14. Classifier: Programming Language :: Python :: 3.4
  15. Classifier: Programming Language :: Python :: 3.5
  16. Classifier: Programming Language :: Python :: 3.6
  17. Requires-Python: >=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*
  18. Description-Content-Type: text/markdown
  19. PySocks
  20. =======
  21. PySocks lets you send traffic through SOCKS and HTTP proxy servers. It is a modern fork of [SocksiPy](http://socksipy.sourceforge.net/) with bug fixes and extra features.
  22. Acts as a drop-in replacement to the socket module. Seamlessly configure SOCKS proxies for any socket object by calling `socket_object.set_proxy()`.
  23. ----------------
  24. Features
  25. ========
  26. * SOCKS proxy client for Python 2.7 and 3.4+
  27. * TCP supported
  28. * UDP mostly supported (issues may occur in some edge cases)
  29. * HTTP proxy client included but not supported or recommended (you should use urllib2's or requests' own HTTP proxy interface)
  30. * urllib2 handler included. `pip install` / `setup.py install` will automatically install the `sockshandler` module.
  31. Installation
  32. ============
  33. pip install PySocks
  34. Or download the tarball / `git clone` and...
  35. python setup.py install
  36. These will install both the `socks` and `sockshandler` modules.
  37. Alternatively, include just `socks.py` in your project.
  38. --------------------------------------------
  39. *Warning:* PySocks/SocksiPy only supports HTTP proxies that use CONNECT tunneling. Certain HTTP proxies may not work with this library. If you wish to use HTTP (not SOCKS) proxies, it is recommended that you rely on your HTTP client's native proxy support (`proxies` dict for `requests`, or `urllib2.ProxyHandler` for `urllib2`) instead.
  40. --------------------------------------------
  41. Usage
  42. =====
  43. ## socks.socksocket ##
  44. import socks
  45. s = socks.socksocket() # Same API as socket.socket in the standard lib
  46. s.set_proxy(socks.SOCKS5, "localhost") # SOCKS4 and SOCKS5 use port 1080 by default
  47. # Or
  48. s.set_proxy(socks.SOCKS4, "localhost", 4444)
  49. # Or
  50. s.set_proxy(socks.HTTP, "5.5.5.5", 8888)
  51. # Can be treated identical to a regular socket object
  52. s.connect(("www.somesite.com", 80))
  53. s.sendall("GET / HTTP/1.1 ...")
  54. print s.recv(4096)
  55. ## Monkeypatching ##
  56. To monkeypatch the entire standard library with a single default proxy:
  57. import urllib2
  58. import socket
  59. import socks
  60. socks.set_default_proxy(socks.SOCKS5, "localhost")
  61. socket.socket = socks.socksocket
  62. urllib2.urlopen("http://www.somesite.com/") # All requests will pass through the SOCKS proxy
  63. 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.
  64. ## urllib2 Handler ##
  65. Example use case with the `sockshandler` urllib2 handler. Note that you must import both `socks` and `sockshandler`, as the handler is its own module separate from PySocks. The module is included in the PyPI package.
  66. import urllib2
  67. import socks
  68. from sockshandler import SocksiPyHandler
  69. opener = urllib2.build_opener(SocksiPyHandler(socks.SOCKS5, "127.0.0.1", 9050))
  70. print opener.open("http://www.somesite.com/") # All requests made by the opener will pass through the SOCKS proxy
  71. --------------------------------------------
  72. Original SocksiPy README attached below, amended to reflect API changes.
  73. --------------------------------------------
  74. SocksiPy
  75. A Python SOCKS module.
  76. (C) 2006 Dan-Haim. All rights reserved.
  77. See LICENSE file for details.
  78. *WHAT IS A SOCKS PROXY?*
  79. A SOCKS proxy is a proxy server at the TCP level. In other words, it acts as
  80. a tunnel, relaying all traffic going through it without modifying it.
  81. SOCKS proxies can be used to relay traffic using any network protocol that
  82. uses TCP.
  83. *WHAT IS SOCKSIPY?*
  84. This Python module allows you to create TCP connections through a SOCKS
  85. proxy without any special effort.
  86. It also supports relaying UDP packets with a SOCKS5 proxy.
  87. *PROXY COMPATIBILITY*
  88. SocksiPy is compatible with three different types of proxies:
  89. 1. SOCKS Version 4 (SOCKS4), including the SOCKS4a extension.
  90. 2. SOCKS Version 5 (SOCKS5).
  91. 3. HTTP Proxies which support tunneling using the CONNECT method.
  92. *SYSTEM REQUIREMENTS*
  93. Being written in Python, SocksiPy can run on any platform that has a Python
  94. interpreter and TCP/IP support.
  95. This module has been tested with Python 2.3 and should work with greater versions
  96. just as well.
  97. INSTALLATION
  98. -------------
  99. Simply copy the file "socks.py" to your Python's `lib/site-packages` directory,
  100. and you're ready to go. [Editor's note: it is better to use `python setup.py install` for PySocks]
  101. USAGE
  102. ------
  103. First load the socks module with the command:
  104. >>> import socks
  105. >>>
  106. The socks module provides a class called `socksocket`, which is the base to all of the module's functionality.
  107. The `socksocket` object has the same initialization parameters as the normal socket
  108. object to ensure maximal compatibility, however it should be noted that `socksocket` will only function with family being `AF_INET` and
  109. type being either `SOCK_STREAM` or `SOCK_DGRAM`.
  110. Generally, it is best to initialize the `socksocket` object with no parameters
  111. >>> s = socks.socksocket()
  112. >>>
  113. The `socksocket` object has an interface which is very similiar to socket's (in fact
  114. the `socksocket` class is derived from socket) with a few extra methods.
  115. To select the proxy server you would like to use, use the `set_proxy` method, whose
  116. syntax is:
  117. set_proxy(proxy_type, addr[, port[, rdns[, username[, password]]]])
  118. Explanation of the parameters:
  119. `proxy_type` - The type of the proxy server. This can be one of three possible
  120. choices: `PROXY_TYPE_SOCKS4`, `PROXY_TYPE_SOCKS5` and `PROXY_TYPE_HTTP` for SOCKS4,
  121. SOCKS5 and HTTP servers respectively. `SOCKS4`, `SOCKS5`, and `HTTP` are all aliases, respectively.
  122. `addr` - The IP address or DNS name of the proxy server.
  123. `port` - The port of the proxy server. Defaults to 1080 for socks and 8080 for http.
  124. `rdns` - This is a boolean flag than modifies the behavior regarding DNS resolving.
  125. If it is set to True, DNS resolving will be preformed remotely, on the server.
  126. If it is set to False, DNS resolving will be preformed locally. Please note that
  127. setting this to True with SOCKS4 servers actually use an extension to the protocol,
  128. called SOCKS4a, which may not be supported on all servers (SOCKS5 and http servers
  129. always support DNS). The default is True.
  130. `username` - For SOCKS5 servers, this allows simple username / password authentication
  131. with the server. For SOCKS4 servers, this parameter will be sent as the userid.
  132. This parameter is ignored if an HTTP server is being used. If it is not provided,
  133. authentication will not be used (servers may accept unauthenticated requests).
  134. `password` - This parameter is valid only for SOCKS5 servers and specifies the
  135. respective password for the username provided.
  136. Example of usage:
  137. >>> s.set_proxy(socks.SOCKS5, "socks.example.com") # uses default port 1080
  138. >>> s.set_proxy(socks.SOCKS4, "socks.test.com", 1081)
  139. After the set_proxy method has been called, simply call the connect method with the
  140. traditional parameters to establish a connection through the proxy:
  141. >>> s.connect(("www.sourceforge.net", 80))
  142. >>>
  143. Connection will take a bit longer to allow negotiation with the proxy server.
  144. Please note that calling connect without calling `set_proxy` earlier will connect
  145. without a proxy (just like a regular socket).
  146. Errors: Any errors in the connection process will trigger exceptions. The exception
  147. may either be generated by the underlying socket layer or may be custom module
  148. exceptions, whose details follow:
  149. class `ProxyError` - This is a base exception class. It is not raised directly but
  150. rather all other exception classes raised by this module are derived from it.
  151. This allows an easy way to catch all proxy-related errors. It descends from `IOError`.
  152. All `ProxyError` exceptions have an attribute `socket_err`, which will contain either a
  153. caught `socket.error` exception, or `None` if there wasn't any.
  154. class `GeneralProxyError` - When thrown, it indicates a problem which does not fall
  155. into another category.
  156. * `Sent invalid data` - This error means that unexpected data has been received from
  157. the server. The most common reason is that the server specified as the proxy is
  158. not really a SOCKS4/SOCKS5/HTTP proxy, or maybe the proxy type specified is wrong.
  159. * `Connection closed unexpectedly` - The proxy server unexpectedly closed the connection.
  160. This may indicate that the proxy server is experiencing network or software problems.
  161. * `Bad proxy type` - This will be raised if the type of the proxy supplied to the
  162. set_proxy function was not one of `SOCKS4`/`SOCKS5`/`HTTP`.
  163. * `Bad input` - This will be raised if the `connect()` method is called with bad input
  164. parameters.
  165. class `SOCKS5AuthError` - This indicates that the connection through a SOCKS5 server
  166. failed due to an authentication problem.
  167. * `Authentication is required` - This will happen if you use a SOCKS5 server which
  168. requires authentication without providing a username / password at all.
  169. * `All offered authentication methods were rejected` - This will happen if the proxy
  170. requires a special authentication method which is not supported by this module.
  171. * `Unknown username or invalid password` - Self descriptive.
  172. class `SOCKS5Error` - This will be raised for SOCKS5 errors which are not related to
  173. authentication.
  174. The parameter is a tuple containing a code, as given by the server,
  175. and a description of the
  176. error. The possible errors, according to the RFC, are:
  177. * `0x01` - General SOCKS server failure - If for any reason the proxy server is unable to
  178. fulfill your request (internal server error).
  179. * `0x02` - connection not allowed by ruleset - If the address you're trying to connect to
  180. is blacklisted on the server or requires authentication.
  181. * `0x03` - Network unreachable - The target could not be contacted. A router on the network
  182. had replied with a destination net unreachable error.
  183. * `0x04` - Host unreachable - The target could not be contacted. A router on the network
  184. had replied with a destination host unreachable error.
  185. * `0x05` - Connection refused - The target server has actively refused the connection
  186. (the requested port is closed).
  187. * `0x06` - TTL expired - The TTL value of the SYN packet from the proxy to the target server
  188. has expired. This usually means that there are network problems causing the packet
  189. to be caught in a router-to-router "ping-pong".
  190. * `0x07` - Command not supported - For instance if the server does not support UDP.
  191. * `0x08` - Address type not supported - The client has provided an invalid address type.
  192. When using this module, this error should not occur.
  193. class `SOCKS4Error` - This will be raised for SOCKS4 errors. The parameter is a tuple
  194. containing a code and a description of the error, as given by the server. The
  195. possible error, according to the specification are:
  196. * `0x5B` - Request rejected or failed - Will be raised in the event of an failure for any
  197. reason other then the two mentioned next.
  198. * `0x5C` - request rejected because SOCKS server cannot connect to identd on the client -
  199. The Socks server had tried an ident lookup on your computer and has failed. In this
  200. case you should run an identd server and/or configure your firewall to allow incoming
  201. connections to local port 113 from the remote server.
  202. * `0x5D` - request rejected because the client program and identd report different user-ids -
  203. The Socks server had performed an ident lookup on your computer and has received a
  204. different userid than the one you have provided. Change your userid (through the
  205. username parameter of the set_proxy method) to match and try again.
  206. class `HTTPError` - This will be raised for HTTP errors. The message will contain
  207. the HTTP status code and provided error message.
  208. After establishing the connection, the object behaves like a standard socket.
  209. Methods like `makefile()` and `settimeout()` should behave just like regular sockets.
  210. Call the `close()` method to close the connection.
  211. In addition to the `socksocket` class, an additional function worth mentioning is the
  212. `set_default_proxy` function. The parameters are the same as the `set_proxy` method.
  213. This function will set default proxy settings for newly created `socksocket` objects,
  214. in which the proxy settings haven't been changed via the `set_proxy` method.
  215. This is quite useful if you wish to force 3rd party modules to use a SOCKS proxy,
  216. by overriding the socket object.
  217. For example:
  218. >>> socks.set_default_proxy(socks.SOCKS5, "socks.example.com")
  219. >>> socket.socket = socks.socksocket
  220. >>> urllib.urlopen("http://www.sourceforge.net/")
  221. PROBLEMS
  222. ---------
  223. Please open a GitHub issue at https://github.com/Anorov/PySocks