对于Python请求,如何确定从服务器返回的位置标头的值?如果我有一个简单的the服务器
from flask import Flask, Response, request
def root():
return Response(headers={
'location': 'http://some.domain.com/?' # Note the ?
})
app = Flask('app')
app.add_url_rule('/', view_func=root)
app.run(host='0.0.0.0', port=8081, debug=False)
然后跑
import requests
response = requests.get('http://localhost:8081/', allow_redirects=False)
print(response.headers['location'])
我明白了
http://some.domain.com/
在/
之后没有问号
这与Flask request: determine exact path, including if there is a question mark有关。我使用Python请求来测试返回重定向的应用程序,但我意识到请求正在从位置标头中删除尾随问号。
发布于 2019-08-10 10:17:55
这是一个红鲱鱼:请求并没有从位置标头上去掉问号。如果将烧瓶服务器更改为返回两个完全相同的头,一个location
和一个test
。
def root():
return Response(headers={
'location': 'http://some.domain.com/?', # Note the ?
'test': 'http://some.domain.com/?', # Note the ?
})
我们通过套接字发出一个原始的HTTP请求
import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(('127.0.0.1', 8081))
request = \
f'GET / HTTP/1.1\r\n' \
f'host:127.0.0.1\r\n' \
f'\r\n'
sock.send(request.encode())
response = b''
while b'\r\n\r\n' not in response:
response += sock.recv(4096)
sock.close()
print(response)
响应包含带有test
的?
头。
test: http://some.domain.com/?\r\n
但是没有location
的?
头
Location: http://some.domain.com/\r\n
因此,似乎正在操作返回的位置标头的是Flask (或服务器中使用的其他组件之一)。
https://stackoverflow.com/questions/57440682
复制相似问题