博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Flask的SERVER_NAME解析
阅读量:7088 次
发布时间:2019-06-28

本文共 4360 字,大约阅读时间需要 14 分钟。

hot3.png

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

Flask中的SERVER_NAME主要做两件事:

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

  2. 用于子域名支持

很多人误以为它可以做这两件事之外的其它事情。

第一件事:绝对URL

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

下面举个例子演示一下:

# filename myapp.pyfrom flask import Flask, url_forapp = 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/

可以看出,在未设置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 independent URL generation. You might be able to fix this by setting the SERVER_NAMEconfig 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的扩展都是假定自己运行在一个活动的应用和请求上下文中,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 Blueprintpublic = Blueprint('public', __name__)@public.route('/')def home():    return 'hello flask'

app.py:

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

现在可以通过public.example.com/来访问public模块了。

通配符子域

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

先来看段示例代码:

modules.py:

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

app.py:

app = Flask(__name__)app.config['SERVER_NAME'] = 'example.com'from modules import memberapp.register_blueprint(member, 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_requestdef 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服务等),并做好泛域名解析,然后再进行调试。当然使用公网域名和服务器来调试也未尝不可。

英文好的同学可以参阅:

转载于:https://my.oschina.net/935572630/blog/371447

你可能感兴趣的文章
rsync+inotify配置
查看>>
我的友情链接
查看>>
Spring Security 杂记2
查看>>
Android学习笔记系列
查看>>
Struts原理
查看>>
@SuppressWarnings("serial")在Java中有什么作用?
查看>>
PropertyPlaceholderConfigurer 和PropertiesFactoryBean区别
查看>>
unable to parse template class 'Text Files' 解决
查看>>
强大的bat文件搞定系统所有问题
查看>>
程序员常用快捷键
查看>>
0216_帧中继_ipsec
查看>>
play(三) play实例项目开发Yabe 2前端页面
查看>>
天猫商城的五大购物优势
查看>>
数据结构——栈
查看>>
rocketmq之源码分析源码构建(二)
查看>>
[转载] New Concept English 1——Lesson 2 Is this your...?
查看>>
Java IO_Socket
查看>>
我的友情链接
查看>>
[转载] 七龙珠第一部——第080话 悟空对抗天龙
查看>>
Facebook账号被锁定怎么找回
查看>>