Flask中的 SERVER_NAME 主要做两件事:
协助Flask在活动的请求(request)之外生成绝对URL(比如邮件中嵌入网站URL)
用于子域名支持
很多人误以为它可以做这两件事之外的其它事情。
第一件事:绝对URL
我们知道, url_for 默认情况下是生成相对URL,它有个参数 _external ,如果设置为真,则会生成一个绝对URL(就是HTTP开头带域名等信息的)。若不指定SERVER_NAME ,默认使用当前活动的请求(request)来生成URL。
下面举个例子演示一下:
# 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】通过浏览器访问
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/ 。
若我们通过浏览器访问 http://localhost:5000/test ,则返回的内容是:http://localhost:5000/ 。
可以看出,在未设置 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 "", line 2, in
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 using
app.app_context() .
因此,若要生成不依赖于request的绝对URL(比如异步发送邮件时在邮件中生成网站某个页面的URL),就必须要设置 SERVER_NAME 。
第二件事:子域名支持
SERVER_NAME 键是用于子域名支持。因为 Flask 在得知现有服务器名之前不能猜测出子域名部分,所以如果你想使用子域名,这个选项必要的,并且也用于会话cookie。
请牢记不只有 Flask 存在不知道子域名的问题,你的浏览器同样存在这样的问题。 大多数现代 web 浏览器不允许服务器名不含有点的跨子域名 cookie。因此如果你的服务器的 名称为 localhost ,你将不能为 localhost 和所有它的子域名设置一个 cookie。 请选择一个合适的服务器名,像 'myapplication.local ', 并添加你想要的服务器名 + 子域名 到你的 host 配置或设置一个本地 bind。