Python – How to make HTTP request through a (tor) socks proxy using python


I'm trying to make a HTTP request using python. I tried changing my windows system proxy (using inetcpl.cpl )

url = ''
request = urllib2.Request(url)
request.set_proxy('', 'socks')
response = urllib2.urlopen(request)

is giving me the error

Traceback (most recent call last): File "", line 1, in

response = urllib2.urlopen(request) File "C:\Python27\lib\", line 126, in urlopen
return, data, timeout) File "C:\Python27\lib\", line 400, in open
response = self._open(req, data) File "C:\Python27\lib\", line 423, in _open
'unknown_open', req) File "C:\Python27\lib\", line 378, in _call_chain
result = func(*args) File "C:\Python27\lib\", line 1240, in unknown_open
raise URLError('unknown url type: %s' % type) URLError:

Best Solution

I'm the OP. According to the answer given by Tisho, this worked for me:

import urllib, urllib2

##Download SocksiPy - A Python SOCKS client module. ( )
##Simply copy the file "" to your Python's lib/site-packages directory, and initiate a socks socket like this.
## NOTE: you must use socks before urllib2.
import socks
import socket
socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, "", 9050)
socket.socket = socks.socksocket

url = ''
request = urllib2.Request(url)
response = urllib2.urlopen(request)
Related Question