Flask子域名及通配符子域 - digwtx's Blog
Flask子域名及通配符子域
在之前的文章中,我们讲到Flask中的SERVER_NAME
主要做两件事:
- 协助Flask生成请求上下文之外的URL(比如邮件)
- 用于子域名支持
今天我们就来讲讲子域名这部分。
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
模块了。
下面是为任何Flask
或Blueprint
对象增加子域名支持的便捷函数:
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.subdomain
def 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
本文链接: http://flask123.sinaapp.com/article/45/
版权所有。转载时必须以链接形式注明作者和原始出处及本声明。
2018年10月18日 18:33
really impress your writing system
2018年10月18日 18:34
it is good information website
2018年10月18日 18:35
it is good articel
2018年10月18日 18:36
I invite my friend for program language teach
2018年10月18日 18:37
I will back you website
2019年4月12日 12:26
<a href="http://menards.club/2019/04/01/tm-menards/"> <b> TM Menards | Register, Login and Access TM Menards Account </b> </a> I found this blog very informative and interesting. Will look forward to more such posts <a href="http://menards.club/"> <b> Menards Club </b> </a>
2019年9月24日 17:52
cheap coach hire cheap minibus coach hire emergency minibus hire minibus taxi hire minibus hire with driver minibus hire kent cheap minibus hire local minibus hire drayton manor minibus hire london to luton minibus hire liverpool to manchester minibus hire manchester taxi hire alton towers minibus hire minibus hire crewe minibus hire wolverhampton minibus hire ipswich minibus hire blackpool coach hire peterborough minibus hire leicester thorpe park minibus hire coach hire london minibus hire glasgow minibus hire falkirk minibus hire slough minibus hire dewsbury minibus hire scarborough minibus hire sheffield minibus hire cardiff minibus hire barnsley minibus hire stevenage minibus hire norwich
2020年3月04日 14:28
敬启者:个人小网站希望大家多多支持 感谢您对我们热心的支持 f88tw┃华歌尔┃I appreciate your kind assistance. f88tw|墓园|捡骨流程|捡骨费用|捡骨时间|禁忌|捡骨颜色|捡骨师|新竹|时间|台北|桃园|苗栗|头份|火化|晋塔|安葬|法事|捡骨|看日子|墓穴|墓园|坟墓|看日子|乞丐|http://mypaper.m.pchome.com.tw/f88tw
2021年3月03日 20:24
A company specializing in the maintenance of home appliances in Riyadh and Jeddah
2021年3月03日 20:25
A company specializing in the maintenance of home appliances in Riyadh and Jeddah
2021年3月03日 20:26
A company specializing in the maintenance of home appliances in Riyadh and Jeddah
2021年3月03日 20:27
A company specializing in the maintenance of home appliances in Riyadh and Jeddah
2021年3月23日 15:05
This type of message always inspiring and I prefer to read quality content, so happy to find good place to many here in the post, the writing is just great, thanks for the post.
2021年4月08日 10:47
敬启者:个人小网站希望大家多多支持 感谢您对我们热心的支持 f88tw┃华歌尔┃I appreciate your kind assistance. f88tw| 粗工| 粗工内容 | 粗工| 粗工内容 |墓园|捡骨流程|捡骨费用|捡骨时间|禁忌|捡骨颜色|捡骨师|新竹|时间|台北|桃园|苗栗|头份|https://mypaper.m.pchome.com.tw/f88tw