gpt4 book ai didi

python - urllib2 代理不适用于 tor

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:39:14 26 4
gpt4 key购买 nike

我想用 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com