我对模块请求返回的HTTP标头有问题。请求HTTP标头
我用下面的代码:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import requests
response = requests.get("http://www.google.co.il",proxies={'http': '','https':''})
data = response.text
# response.text returns the appropriate html code
# (<!doctype html><html dir="rtl" itemscope=""....)
if response.status_code == requests.codes.ok:
# How do I send those headers to the conn (browser)
print "HEADERS: " + str(response.headers)
conn.send(data)
我试图发送一个GET请求到www.google.co.il,并传送到浏览器的响应(在我打电话的例子它“conn”)。问题在于浏览器不会显示收到的HTML代码,而是收到ERR_EMPTY_RESPONSE。 在响应标头是:
HEADERS: {'Content-Length': '5451', 'X-XSS-Protection': '1; mode=block', 'Content-Encoding': 'gzip', 'Set-Cookie': 'NID=103=RJzu4RTCNxkh-75dvKBHx-_jen9M8iPes_AdOIQqzBVZ0VPTz1PlQaAVLpwYOmxZlTKmcogiDb1VoY__Es0HqSNwlkmHl3SuBZC8_8XUfqh1PzdWTjrXRnB4S738M1lm; expires=Wed, 08-Nov-2017 10:05:46 GMT; path=/; domain=.google.co.il; HttpOnly', 'Expires': '-1', 'Server': 'gws', 'Cache-Control': 'private, max-age=0', 'Date': 'Tue, 09 May 2017 10:05:46 GMT', 'P3P': 'CP="This is not a P3P policy! See https://www.google.com/support/accounts/answer/151657?hl=en for more info."', 'Content-Type': 'text/html; charset=windows-1255', 'X-Frame-Options': 'SAMEORIGIN'}
有人告诉我,问题是,我不发送任何头到浏览器。这真的是问题吗?还有其他建议吗?如果这是问题,我该如何将适当的标题发送给浏览器?
编辑:我忘了提及通过代理服务器的连接是 。
任何帮助将是伟大的!
非常感谢Yahli。
什么是'conn'?套接字? –
是的,'conn'是一个插座。 –
有点像拦截代理权吗?您必须向浏览器发送完整的HTTP响应,而不仅仅是正文。如果您使用套接字,则可以跳过“请求”并使用“套接字”发送HTTP请求。如果你坚持使用'requests',你必须从'response'对象构造头部。 –