Alex Rivera | Logout

How can I use a SOCKS 4/5 proxy with urllib2?

Asked 2010-02-23T11:55:57.610
48

How can I use a SOCKS 4/5 proxy with urllib2 to download a web page?

Edit
Report

1 Answer

68

You can use 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. (Try it pip install PySocks )

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.

answered 2010-02-26T03:06:52.037

Your Answer