请求 HTTP headers
Requests HTTP headers
我遇到了模块请求 returns 的 HTTP header 的问题。
我正在使用以下代码:
#!/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)
我正在尝试向 www.google.co.il 发送 GET 请求,并将响应发送到浏览器(在我称之为 "conn" 的示例中)。问题是浏览器不会显示收到的 HTML 代码,而是我收到 ERR_EMPTY_RESPONSE。
响应中的 header 是:
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'}
有人告诉我问题是我没有向浏览器发送任何 header。这真的是问题所在吗?还有其他建议吗?如果是问题所在,我该如何将适当的 header 发送到浏览器?
编辑:我忘了说连接是
通过代理服务器。
任何帮助都会很棒!
非常感谢,Yahli。
我在 requests
文档中找不到有关获取原始 http 响应(不是 response.raw
)的任何信息,所以我写了一个函数:
def http_response(response):
return 'HTTP/1.1 {} {}\r\n{}\r\n\r\n{}'.format(
response.status_code, response.reason ,
'\r\n'.join(k + ': ' + v for k, v in response.headers.items()),
response.content
)
我通过将 Firefox HTTP 代理设置为 localhost:port(在端口上有一个侦听套接字)来测试它,它工作正常。
或者您可以从 conn.recv
获取主机,打开到该主机的新套接字,然后发送数据。示例:
data = conn.recv(1024)
host = [ l.split(':')[1].strip() for l in data.splitlines() if l.startswith('Host:') ]
if len(host) :
cli = socket.socket()
cli.connect((host[0], 80))
cli.send(data)
response = ''
while True :
data = cli.recv(1024)
if not data.strip() :
break
response += data
conn.send(response)
cli.close()
其中 conn
是与网络浏览器的连接。这只是一个简单的示例,假设您只有 HTTP 请求(端口 80)。还有很大的优化空间
我遇到了模块请求 returns 的 HTTP header 的问题。
我正在使用以下代码:
#!/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)
我正在尝试向 www.google.co.il 发送 GET 请求,并将响应发送到浏览器(在我称之为 "conn" 的示例中)。问题是浏览器不会显示收到的 HTML 代码,而是我收到 ERR_EMPTY_RESPONSE。 响应中的 header 是:
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'}
有人告诉我问题是我没有向浏览器发送任何 header。这真的是问题所在吗?还有其他建议吗?如果是问题所在,我该如何将适当的 header 发送到浏览器?
编辑:我忘了说连接是 通过代理服务器。
任何帮助都会很棒!
非常感谢,Yahli。
我在 requests
文档中找不到有关获取原始 http 响应(不是 response.raw
)的任何信息,所以我写了一个函数:
def http_response(response):
return 'HTTP/1.1 {} {}\r\n{}\r\n\r\n{}'.format(
response.status_code, response.reason ,
'\r\n'.join(k + ': ' + v for k, v in response.headers.items()),
response.content
)
我通过将 Firefox HTTP 代理设置为 localhost:port(在端口上有一个侦听套接字)来测试它,它工作正常。
或者您可以从 conn.recv
获取主机,打开到该主机的新套接字,然后发送数据。示例:
data = conn.recv(1024)
host = [ l.split(':')[1].strip() for l in data.splitlines() if l.startswith('Host:') ]
if len(host) :
cli = socket.socket()
cli.connect((host[0], 80))
cli.send(data)
response = ''
while True :
data = cli.recv(1024)
if not data.strip() :
break
response += data
conn.send(response)
cli.close()
其中 conn
是与网络浏览器的连接。这只是一个简单的示例,假设您只有 HTTP 请求(端口 80)。还有很大的优化空间