How can I use a SOCKS 4/5 proxy with urllib2 to download a web page?
A:
Since SOCKS is a socket level proxy, you have to replace the socket object used by urllib2
. Please take a look a this solution. If monkey patching is not good enough for you, then you can try to subclass or copy-modify the code from the urllib2
standard library.
fviktor
2010-02-23 13:50:59
Thanks for your answer!
Mike
2010-02-27 22:35:21
+3
A:
you can use SocksiPy[http://sourceforge.net/projects/socksipy/] module. Simply copy the file "socks.py" to your Python's lib/site-packages directory, and you're ready to go.
you must use socks before urllib2.
For example:
import socks
import socket
socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, "127.0.0.1", 8080)
socket.socket = socks.socksocket
import urllib2
print urllib2.urlopen('http://www.google.com').read()
you can also try pycurl lib and tsocks, for more detail, click on here.
pan
2010-02-26 03:06:52