python - urllib2 代理不适用于 tor

标签 python linux proxy tor

我想用 python 编写一些脚本,它使用 tor/代理地址来访问 web,为了测试我有以下脚本:

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 而不是 socks 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

最佳答案

我使用 http(不是 sock)并且它有效

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)

关于python - urllib2 代理不适用于 tor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9836504/

相关文章:

ssl - 在 nginx 中为 nextcloud 反向代理?

python - SelectById2 的指针标注

linux - 如何在linux环境下安装pyhs2?

regex - 如何使用此正则表达式匹配最后一个字符串

linux - 在重音符内执行时 sed 奇怪的行为 - `

proxy - 为什么 CNTLM 要求输入密码?

python - NumPy 切片 : All except one array entry

Python 在导入简单的 C 扩展模块时出错

python - 如何在 Pandas 中组合文本行

java - 当 URL 路径中有特殊字符时,将 Apache ProxyPass 或 Mod_jk 与 Tomcat 结合使用