繁体   English   中英

Python3:具有privoxy / TOR的http.client发出错误请求

[英]Python3: http.client with privoxy/TOR making bad requests

我正在尝试将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会将我重定向到我刚刚请求的网址,但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.comhttp://check.torproject.org上获得您的浏览器的确切信息。 我不知道这是怎么回事。 我怀疑问题出在python,因为我可以在firefox中使用TOR,但我真的不知道。

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日志没什么好说的。

最终这是因为我正在连接http://而那些站点需要https:// 对于接受正常http://网站,它确实可以正常工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM