小编Nic*_*las的帖子

使用unicode_literals在Flask应用中添加标头

使用unicode_literals添加标头似乎与Nginx,uWSGI和一个简单的Flask应用程序失败:

# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from flask import Flask, make_response

app = Flask('test')

@app.route('/')
def index():
    response = make_response()
    response.status_code = 401
    response.headers = {'WWW-Authenticate': 'Basic realm="test"'} # Fail
    # response.headers = {b'WWW-Authenticate': b'Basic realm="test"'} # Succeed
    return response

if __name__ == '__main__':
    app.run(debug=True)
Run Code Online (Sandbox Code Playgroud)

该应用程序可直接用于调试目的或通过Nginx - > uWSGI - > Flask并且运行良好.

  • 当我使用浏览器直接连接到应用程序时,我有一个登录对话框, WWW-Authenticate标题是正确的.
  • 通过Nginx的相同请求返回标头Transfert-Encoding: chunked并丢弃WWW-Authenticate标头.

强制b'...') format to add the header make the app works as expected in both …

python unicode nginx flask

5
推荐指数
1
解决办法
713
查看次数

标签 统计

flask ×1

nginx ×1

python ×1

unicode ×1