我正在构建一个简单的Flask
应用程序,我想返回重定向响应。另外,我想保持对标题的完全控制。
这是我到目前为止所得到的:
from flask import Flask
from werkzeug.wrappers import Response
app = Flask(__name__)
@app.route('/toexample')
def to_example():
headers = {
'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:39.0) Gecko/20100101 Firefox/39.0',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Language': 'en-US,en;q=0.5',
'Accept-Encoding': 'gzip, deflate',
}
return Response('www.example.com', status_code=302, headers=headers)
if __name__ == '__main__':
app.run(debug=True)
我收到错误:
TypeError: __init__() got an unexpected keyword argument 'status_code'
好的,status_code
上似乎不存在__init__()
,但这样做的正确方法是什么?
我最终想要的是一个用户可以点击的链接,但我想再次保持对标题的控制(连接,Cookie,Referer等)
答案 0 :(得分:2)
我必须在标头中添加Location
。另外,status_code
是错误的,它应该是status=302
。
工作示例:
from flask import Flask
from werkzeug.wrappers import Response
app = Flask(__name__)
@app.route('/toexample')
def to_example():
headers = {
'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:39.0) Gecko/20100101 Firefox/39.0',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Language': 'en-US,en;q=0.5',
'Accept-Encoding': 'gzip, deflate',
'Location': 'http://www.example.com'
}
return Response('http://www.example.com', status=302, headers=headers)
if __name__ == '__main__':
app.run(debug=True)
答案 1 :(得分:2)
这对我有用:
from flask import Flask, redirect
app = Flask(__name__)
@app.route('/toexample')
def to_example():
response = redirect("http://www.example.com", code=302)
headers = dict(response.headers)
headers.update({'X-Custom-Header1': 'value1', 'X-Custom-Header2': 'value2'})
response.headers = headers
return response
这适用于我Flask
v0.10.1
。干杯!
答案 2 :(得分:0)
你想在这里使用两件事。首先,对于重定向,您希望在redirect
来电中使用to_example
:
@app.route('/toexample')
def to_example():
return redirect("http://www.example.com", code=302)
现在,为了控制自定义标头和Cookie,您可以使用after_request,这样您就可以在向响应对象发出请求后设置某些自定义细节:
@app.after_request
def after_request(response):
response.headers.add('Custom-Header', 'Custom Header')
response.headers.add('Content-Type', 'application/json')
response.set_cookie('some-cookie', value='some-cookie-value')
return response
总而言之,您的示例现在看起来像这样:
from flask import Flask, redirect
app = Flask(__name__)
@app.route('/toexample')
def to_example():
return redirect("http://www.example.com", code=302)
@app.after_request
def after_request(response):
response.headers.add('Custom-Header', 'Custom Header')
response.headers.add('Content-Type', 'application/json')
response.set_cookie('some-cookie', value='some-cookie-value')
return response
if __name__ == '__main__':
app.run(debug=True)
这是一个卷曲电话。请注意标题。
▶ curl -v http://127.0.0.1:5000/toexample
* Trying 127.0.0.1...
* Connected to 127.0.0.1 (127.0.0.1) port 5000 (#0)
> GET /toexample HTTP/1.1
> Host: 127.0.0.1:5000
> User-Agent: curl/7.43.0
> Accept: */*
>
* HTTP 1.0, assume close after body
< HTTP/1.0 302 FOUND
< Content-Type: text/html; charset=utf-8
< Content-Length: 251
< Location: http://www.example.com
< Custom-Header: Custom Header
< Content-Type: application/json
< Set-Cookie: some-cookie=some-cookie-value; Path=/
< Server: Werkzeug/0.10.4 Python/3.5.0
< Date: Sun, 01 Nov 2015 23:11:30 GMT
<
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>Redirecting...</title>
<h1>Redirecting...</h1>
* Closing connection 0
<p>You should be redirected automatically to target URL: <a href="http://www.example.com">http://www.example.com</a>. If not click the link.%