Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/89.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 具有代理连接的HTTP/s流_Python_Html_Http_Https_Proxy - Fatal编程技术网

Python 具有代理连接的HTTP/s流

Python 具有代理连接的HTTP/s流,python,html,http,https,proxy,Python,Html,Http,Https,Proxy,这几天我在写一份远期委托书 我在HTTP/s代码方面遇到了三个问题。 第一个问题是,每当我试图处理重定向[302](例如,从www.google.com)时,我不会得到任何数据。 我正在使用以下代码处理重定向: try: response = requests.get("http://"+webserver, timeout=2) if response.history: print "Redirected to " + response.url

这几天我在写一份远期委托书

我在HTTP/s代码方面遇到了三个问题。 第一个问题是,每当我试图处理重定向[302](例如,从www.google.com)时,我不会得到任何数据。 我正在使用以下代码处理重定向:

try:
    response = requests.get("http://"+webserver, timeout=2)
    if response.history:
        print "Redirected to " + response.url
        c = httplib.HTTPSConnection(response.url, port,config['CONNECTION_TIMEOUT'])
    else:
        print "Getting information from " + webserver
        c = httplib.HTTPSConnection(webserver, port,config['CONNECTION_TIMEOUT'])
except requests.exceptions.ConnectionError:
    print "Getting information from " + webserver
    c = httplib.HTTPSConnection(webserver)
如果我试图通过直接连接到重定向url来绕过第一个错误,则会出现第二个问题。我从网站(任何受http/s保护的网站,例如www.google.co.il)收到的数据包含不需要的字符,如问号:����� 而不是希伯来语或阿拉伯语(英语很好)

我使用以下代码处理数据:

               c = httplib.HTTPSConnection(webserver)
               while 1:
                    c.request("GET", "/")
                    response = c.getresponse()
                    # send request to web server
                    # Indiscriminately forward bytes
                    data = response.read()  # NEED TO DECODE
                    print "DATA : ", data
                    if len(data) > 0:
                        conn.send(data)
                        print "DATA SENT!"
                    else:
                        break
                c.close()
                conn.close()
这是我收到的数据(我不能在这里包括所有内容,所以这是一张带有数据html预览的照片):

我遇到的最后一个问题是数据没有发送到浏览器。代理接收数据(如第二个问题中所示),但由于某些原因,它没有将数据转发到浏览器。(代码与第二个问题相同)

浏览器显示此错误:错误\u隧道\u连接\u失败

任何帮助都将不胜感激

提前谢谢你,亚莉


*编辑:仍然找不到答案。我需要您的帮助:)

您正在从套接字读取的是原始字节。我认为您需要先将它们放入UTF-8,如下所示:

data = response.read().decode('utf8')
请注意,内容并非总是以UTF-8编码。必须为每个响应分别检查
内容类型
HTTP头


关于您的浏览器问题:我怀疑您忘记了向客户端发送适当的HTTP头。或者,事实上,任何类型的标题部分。

如果你不想让谷歌重定向到你的国家域名,请使用它,它会指示你,不幸的是,这对我没有多大帮助。首先,我需要在其他问题上得到进一步的帮助。唉,我已经阅读了更多关于这个主题的内容,并且我已经解决了一些问题,谢谢!仅供参考,模块请求通过根据Content-Type报头解除对编码的限制来处理编码,因此无需再次编码:)