Say the socket agent, first of all, to say that the HTTP proxy, Python urllib2 is a self-contained HTTP proxy function, you can use the following code to achieve:
Copy Code code as follows:
Proxy_handler = Urllib2. Proxyhandler ({' http ': ' http://Address: Port '})
Opener = Urllib2.build_opener (Proxy_handler, Urllib2. HttpHandler)
Urllib2.install_opener (opener)
So, the next time you use URLLIB2 to Urlopen, you will automatically use this agent.
However, in our crazy celestial, sometimes, HTTP proxy does not meet the requirements, perhaps you want to use the lower socket proxy, so Python seems to have no direct method (Python3 do not know whether there is no built-in, not research). At this time, you may need this package: Socksipy–a Python SOCKS module, download decompression, in addition to the document also on a socks.py file, very convenient to use, to produce sample code:
Copy Code code as follows:
Import socks, Socket
Socks.setdefaultproxy (socks. PROXY_TYPE_SOCKS5, "Address", Port)
Socket.socket = Socks.socksocket
Next, you can use the socket as usual, everything will be quietly through the agent out of the ~