关于python:Flask Redirect()发送无效的主机

Flask redirect() sends invalid host

我正在取出一个旧的Flask应用程序,并试图使其运行。但是,flask.redirect()使我有些头疼。这以前曾经起作用过,也许在最近的Flask版本中有所改变?

当我访问应用程序索引页面(/)时,由于索引视图上的requires_auth()装饰器,它应该将我重定向到/login(请参见下文)。它确实会重定向我,但是主机不正确。而不是结束于flux.example.com/login,我结束于flux/login

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
def requires_auth(func):
  ''' Decorator for view functions that require basic authentication. '''

  from .models import Session, User

  @functools.wraps(func)
  def wrapper(*args, **kwargs):
    app.logger.warn('Checking authentication ..')  # added for debug purposes just now
    app.logger.warn('Note: url_for(\'login\'): {}'.format(url_for('login')))
    user_name = session.get('user_name')
    user_passhash = session.get('user_passhash')
    with Session() as db_session:
      user = User.get_by(db_session, user_name, user_passhash)
      if not user:
        return redirect(url_for('login'))

    request.user = user
    return func(*args, **kwargs)

  return wrapper

这是响应头:

1
2
3
4
5
6
Connection:keep-alive
Content-Length:219
Content-Type:text/html; charset=utf-8
Date:Wed, 19 Apr 2017 21:18:51 GMT
Location:http://flux/login
Server:nginx/1.6.2

在配置中设置SERVER_NAME=https://flux.example.com无效。实际上,整个应用程序都无法正常工作,我访问的任何页面都显示404。

如何解决此重定向问题?

Python 3.6,烧瓶0.10.1


我认为您走在正确的Rails上,但是您应该只使用没有协议的主机名:SERVER_NAME=flux.example.com