How to make HTTP request through a (tor) socks proxy using python? -
i'm trying make http request using python. tried changing windows system proxy (using inetcpl.cpl
)
url = 'http://www.whatismyip.com' request = urllib2.request(url) request.add_header('cache-control','max-age=0') request.set_proxy('127.0.0.1:9050', 'socks') response = urllib2.urlopen(request) response.read()
is giving me error
traceback (most recent call last): file "", line 1, in response = urllib2.urlopen(request) file "c:\python27\lib\urllib2.py", line 126, in urlopen return _opener.open(url, data, timeout) file "c:\python27\lib\urllib2.py", line 400, in open response = self._open(req, data) file "c:\python27\lib\urllib2.py", line 423, in _open 'unknown_open', req) file "c:\python27\lib\urllib2.py", line 378, in _call_chain result = func(*args) file "c:\python27\lib\urllib2.py", line 1240, in unknown_open raise urlerror('unknown url type: %s' % type) urlerror:
i'm op. according answer given tisho, worked me:
import urllib, urllib2 ##download socksipy - python socks client module. ( http://code.google.com/p/socksipy-branch/downloads/list ) ##simply copy file "socks.py" python's lib/site-packages directory, , initiate socks socket this. ## note: must use socks before urllib2. import socks import socket socks.setdefaultproxy(socks.proxy_type_socks5, "127.0.0.1", 9050) socket.socket = socks.socksocket url = 'http://ifconfig.me/ip' request = urllib2.request(url) request.add_header('cache-control','max-age=0') response = urllib2.urlopen(request) print response.read()
Comments
Post a Comment