【发布时间】:2023-03-10 13:05:02
【问题描述】:
我正在尝试将 TOR 与 http.client.HTTPConnection 一起使用,但由于某种原因,我不断收到来自所有内容的奇怪响应。我不太确定如何解释,所以这里是我所拥有的一个例子:
class Socket(http.client.HTTPConnection):
def __init__(self, url):
super().__init__('127.0.0.1', 8118)
super().set_tunnel(url)
#super().__init__(url)
def get(self, url = '/', params = {}):
params = util.params_to_query(params)
if params:
if url.find('?') == -1: url += '?' + params
else: url += '&' + params
self.request(
'GET',
url,
'',
{'Connection': 'Keep alive'}
)
return self.getresponse().read().decode('utf-8')
如果我运行这个:
sock = Socket('www.google.com')
print(sock.get())
我明白了:
<html><head><meta content="text/html;charset=utf-8" http-equiv="content-type"/>
<title>301 Moved</title></head><body>
<h1>301 Moved</h1>
The document has moved
<a href="http://www.google.com:8118/">here</a>.
</body></html>
Google 将我重定向到我刚刚请求的 url,除了 privoxy 端口。它变得更奇怪 - 如果我尝试https://check.torproject.org:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<html>
<head>
<title>Welcome to sergii!</title>
</head>
<body>
<h1>Welcome to sergii!</h1>
This is sergii, a system run by and for the <a href="http://www.torproject.org/">Tor Project</a>.
She does stuff.
What kind of stuff and who our kind sponsors are you might learn on
<a href="http://db.torproject.org/machines.cgi?host=sergii">db.torproject.org</a>.
<p>
</p><hr noshade=""/>
<font size="-1">torproject-admin</font>
</body>
</html>
如果我不尝试使用 privoxy/TOR,我会得到您的浏览器在 http://www.google.com 或 http://check.torproject.org 得到的确切信息。我不知道这里发生了什么。我怀疑问题出在 python 上,因为我可以将 TOR 与 firefox 一起使用,但我真的不知道。
Privoxy 日志读取:
2015-06-27 19:28:26.950 7f58f4ff9700 Request: www.google.com:80/
2015-06-27 19:30:40.360 7f58f4ff9700 Request: check.torproject.org:80/
TOR日志没什么好说的。
【问题讨论】:
-
为什么这个结果很奇怪?你期待看到什么?
-
与您的浏览器获取 google.com 或 check.torproject.org 的内容相同。我可能应该澄清一下,如果我不尝试使用 TOR,我实际上确实得到了这些。
标签: python python-3.x tor http.client