Flask的SERVER_NAME解析

SERVER_NAME是Flask中比较容易用错的一个设置值,本文将介绍如何正确使用SERVER_NAMEjavascript

Flask中的SERVER_NAME主要作两件事:java

  1. 协助Flask在活动的请求(request)以外生成绝对URL(好比邮件中嵌入网站URL)python

  2. 用于子域名支持web

不少人误觉得它能够作这两件事以外的其它事情。flask

第一件事:绝对URL

咱们知道,url_for默认状况下是生成相对URL,它有个参数_external,若是设置为真,则会生成一个绝对URL(就是HTTP开头带域名等信息的)。若不指定SERVER_NAME,默认使用当前活动的请求(request)来生成URL。segmentfault

下面举个例子演示一下:浏览器

# filename myapp.py
from flask import Flask, url_for

app = Flask(__name__)
@app.route('/')
def index():
    return 'hello flask'
@app.route('/test')
def test():
    return url_for('index', _external=True)
if __name__ == '__main__':
    app.run(debug=True)

【情景1】经过浏览器访问ruby

app运行以后,在本地5000端口监听。服务器

(env) F:\tmp>python myapp.py
 * Running on http://127.0.0.1:5000/
 * Restarting with reloader

若咱们经过浏览器访问http://127.0.0.1:5000/test,则返回的内容是:http://127.0.0.1:5000/cookie

能够看出,在未设置SERVER_NAME的状况下,url_for生成的绝对URL是依赖于请求的URL的。下面咱们来看看不经过浏览器访问的状况。

【情景2】非浏览器访问

这个情景是指不存在request请求的状况。

咱们经过Python Shell来模拟:

>>> from myapp import app
>>> with app.app_context():
...     print url_for('index', _external=True)
...
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
  File "F:\tmp\env\lib\site-packages\flask\helpers.py", line 287, in url_for    raise RuntimeError('Application was not able to create a URL 'RuntimeError: Application was not able to create a URL adapter for request indep
endent URL generation. You might be able to fix this by setting the SERVER_NAME
config variable.

上面的意思是说应用程序不能建立一个用于与request不相关的URL生成的URL适配器,能够经过设置SERVER_NAME来解决这个问题。

好,下面咱们为SERVER_NAME设置一个值以后再试试:

>>> app.config['SERVER_NAME'] = 'example.com'
>>> with app.app_context():
...     print url_for('index', _external=True)
...http://example.com/

PS: 通常SERVER_NAME设置为网站的域名。

Flask-Mail相关的文章中有这么一段话:

许多Flask的扩展都是假定本身运行在一个活动的应用和请求上下文中,Flask-Mail
 的send函数使用到current_app这个上下文了,因此当mail.send()函数在一个
 线程中执行的时候须要人为的建立一个上下文,全部在send_async_email中使用了
app.app_context()来建立一个上下文。

原文以下:

Many Flask extensions operate under the assumption that there are active
 application and request contexts. Flask-Mail's send() function uses
current_app, so it requires the application context to be active.  But
 when the mail.send() function executes in a different thread, the
 application context needs to be created artificially usingapp.app_context().

所以,若要生成不依赖于request的绝对URL(好比异步发送邮件时在邮件中生成网站某个页面的URL),就必需要设置SERVER_NAME

第二件事:子域名支持

SERVER_NAME键是用于子域名支持。由于 Flask 在得知现有服务器名以前不能猜想出子域名部分,因此若是你想使用子域名,这个选项必要的,而且也用于会话cookie。

请牢记不仅有 Flask 存在不知道子域名的问题,你的浏览器一样存在这样的问题。 大多数现代 web 浏览器不容许服务器名不含有点的跨子域名 cookie。所以若是你的服务器的 名称为 localhost,你将不能为 localhost 和全部它的子域名设置一个 cookie。 请选择一个合适的服务器名,像 'myapplication.local', 并添加你想要的服务器名 + 子域名 到你的 host 配置或设置一个本地 bind。

以前的文章中,咱们讲到Flask中的SERVER_NAME主要作两件事:

  1. 协助Flask生成请求上下文以外的URL(好比邮件)

  2. 用于子域名支持

今天咱们就来说讲子域名这部分。

Flask子域名

通常用于数量比较少的子域名,一个模块对应一个子域名。先看下面一个例子:

modules.py:

from flask import Blueprint

public = Blueprint('public', __name__)
@public.route('/')
def home():
    return 'hello flask'

app.py:

app = Flask(__name__)
app.config['SERVER_NAME'] = 'example.com'
from modules import public
app.register_blueprint(public, subdomain='public')

如今能够经过public.example.com/来访问public模块了。

通配符子域

通配符子域,即经过一个模块来匹配不少个子域名。好比某些网站提供的个性化域名功能,就是这种形式。

先来看段示例代码:

modules.py:

from flask import Blueprint

member = Blueprint('member', __name__)
@member.route('/')
def home():
    return g.subdomain

app.py:

app = Flask(__name__)
app.config['SERVER_NAME'] = 'example.com'
from modules import member
app.register_blueprint(member, subdomain='<subdomain>')

这段代码和上一节的第像,不一样之处是这里的subdomain使用了动态参数<subdomain>(路由中的URL变量也是这种方式)。咱们能够用这个参数在请求回调函数以前利用的组合的url处理器来获取相关的用户。这样咱们就能够经过*.example.com的形式来访问member模块了。

下面是为任何FlaskBlueprint对象增长子域名支持的便捷函数:

def add_subdomain_to_global(endpoint, values):
    g.subdomain = values.pop('subdomain', None)
def add_subdomain_to_url_params(endpoint, values):
    if not 'subdomain' in values:
        values['subdomain'] = g.subdomaindef 
add_subdomain_support(app):
    app.url_value_preprocessor(add_subdomain_to_global)
    app.url_defaults(add_subdomain_to_url_params)

而后你可使用before_request回调函数来处理子域名:

add_subdomain_support(blueprint)
@blueprint.before_request
def add_user_to_global():
    g.user = None
    if g.subdomain:
        g.user = User.query.filter_by(username=g.subdomain).first_or_404()

注:这里的blueprint请改成实际对象。

特别说明:通配符子域调试不是不太方便,须要作泛域名解析才能够。修改hosts文件来指定域名的方法是不可行 的(子域名较少时能够逐个添加,子域名多了就不太现实了)。本机调试时,能够安装DNS服务器(好比LINUX BIND服务等),并作好泛域名解析,而后再进行调试。固然使用公网域名和服务器来调试也何尝不可。

英文好的同窗能够参阅:Getting bigger with Flask

相关文章
相关标签/搜索