2012-03-23 59 views
0

工作,我想要寫有一些Python腳本使用Tor /代理服務器地址來訪問網絡,爲測試我有以下腳本:的urllib2代理不TOR

import urllib2 
from BeautifulSoup import BeautifulSoup 

protocol = 'socks4' 
ip = '127.0.0.1:9050' 

proxy = urllib2.ProxyHandler({protocol:ip}) 
opener = urllib2.build_opener(proxy) 
urllib2.install_opener(opener) 

page = urllib2.urlopen("http://www.ifconfig.me/ip").read() 

print(page) 

問題是,它顯示了我自己的IP地址,而當直接從終端運行時:

proxychains curl ifconfig.me/ip 

顯示不同的IP,我該如何解決它?

時,HTTP使用的襪子4,而不是它提供了以下錯誤:

Traceback (most recent call last): 
    File "proxy_test.py", line 11, in <module> 
    page = urllib2.urlopen("http://www.ifconfig.me/ip").read() 
    File "/usr/lib/python2.7/urllib2.py", line 126, in urlopen 
    return _opener.open(url, data, timeout) 
    File "/usr/lib/python2.7/urllib2.py", line 400, in open 
    response = meth(req, response) 
    File "/usr/lib/python2.7/urllib2.py", line 513, in http_response 
    'http', request, response, code, msg, hdrs) 
    File "/usr/lib/python2.7/urllib2.py", line 438, in error 
    return self._call_chain(*args) 
    File "/usr/lib/python2.7/urllib2.py", line 372, in _call_chain 
    result = func(*args) 
    File "/usr/lib/python2.7/urllib2.py", line 521, in http_error_default 
    raise HTTPError(req.get_full_url(), code, msg, hdrs, fp) 
urllib2.HTTPError: HTTP Error 501: Tor is not an HTTP Proxy 
+0

HTTP代理爲Tor需要一個代理服務器,比如' polipo'。你有安裝嗎?同樣爲了使用HTTP代理,你必須改變到另一個端口(我的系統上的8118)。 – Dikei 2012-03-23 10:11:19

+0

polipo已安裝 – user873286 2012-03-23 10:29:23

回答

1

我使用HTTP(不套袋)和它的作品

import urllib2 
from BeautifulSoup import BeautifulSoup 

protocol = 'http' 
ip = '127.0.0.1:8118' 

proxy = urllib2.ProxyHandler({protocol:ip}) 
opener = urllib2.build_opener(proxy) 
urllib2.install_opener(opener) 

page = urllib2.urlopen("http://www.ifconfig.me/ip").read() 

print(page)