Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 更改烧瓶中的request.base\u url_Python_Ssl_Request_Flask - Fatal编程技术网

Python 更改烧瓶中的request.base\u url

Python 更改烧瓶中的request.base\u url,python,ssl,request,flask,Python,Ssl,Request,Flask,我在负载平衡后面有一个Flask应用程序,可以终止SSL。我有一段代码,可以在使用SSL时“检测”并改变请求对象: @app.before_request def before_request(): x_forwarded_proto = request.headers.get('X-Forwarded-Proto') if x_forwarded_proto == 'https': request.url = request.url.replace('http

我在负载平衡后面有一个Flask应用程序,可以终止SSL。我有一段代码,可以在使用SSL时“检测”并改变请求对象:

@app.before_request
def before_request():
    x_forwarded_proto = request.headers.get('X-Forwarded-Proto')
    if  x_forwarded_proto == 'https':
        request.url = request.url.replace('http://', 'https://')
        request.url_root = request.url_root.replace('http://', 'https://')
        request.host_url = request.host_url.replace('http://', 'https://')
然后我有一个蓝图视图功能:

admin = Blueprint('admin', __name__, url_prefix='/admin')
@admin.route('/login')
def login():
    print request.url
此函数的输出是(当我转到/admin/login时)始终是http://而不是https://(即使它应该在请求之前的
函数中进行了变异)


关于如何解决这个问题,有什么想法吗?

结果是
请求
是一个代理对象。我不确定内部结构,但每次导入时都会“重置”。我通过对
请求
子类化解决了这个问题

class ProxiedRequest(Request):
    def __init__(self, environ, populate_request=True, shallow=False):
        super(Request, self).__init__(environ, populate_request, shallow)
        # Support SSL termination. Mutate the host_url within Flask to use https://
        # if the SSL was terminated.
        x_forwarded_proto = self.headers.get('X-Forwarded-Proto')
        if  x_forwarded_proto == 'https':
            self.url = self.url.replace('http://', 'https://')
            self.host_url = self.host_url.replace('http://', 'https://')
            self.base_url = self.base_url.replace('http://', 'https://')
            self.url_root = self.url_root.replace('http://', 'https://')

app = Flask(__name__);
app.request_class = ProxiedRequest

您是否检查了if x_forwarded_proto==“https”的计算结果是否为true?@codegeek它的计算结果是否为true,请参阅我的解决方案。这是一种非常错误的方法,只需使用
ProxyFix
中间件,请参阅,在Flask甚至开始从中创建
请求
对象之前,WSGI环境将为您进行修复。这应该是正确的被添加到Flask的默认请求类中。感谢与我们共享。这似乎已添加到Flask中。我正在使用Flask==0.10.1的书籍,就像该功能在中作为。