Flask子域名及通配符子域 - digwtx's Blog

Flask子域名及通配符子域

digwtx posted @ 2014年12月22日 10:17 in 未分类 with tags subdomain flask , 4552 阅读

之前的文章中,我们讲到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.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/ 
版权所有。转载时必须以链接形式注明作者和原始出处及本声明。

Avatar_small
mobile price in bd 说:
2018年10月18日 18:33

really impress your writing system

Avatar_small
nokia mobile price i 说:
2018年10月18日 18:36

I invite my friend for program language teach

Avatar_small
menardsclub 说:
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>

Avatar_small
f88tw┃华歌尔 说:
2020年3月04日 14:28

敬启者:个人小网站希望大家多多支持 感谢您对我们热心的支持 f88tw┃华歌尔┃I appreciate your kind assistance. f88tw|墓园|捡骨流程|捡骨费用|捡骨时间|禁忌|捡骨颜色|捡骨师|新竹|时间|台北|桃园|苗栗|头份|火化|晋塔|安葬|法事|捡骨|看日子|墓穴|墓园|坟墓|看日子|乞丐|http://mypaper.m.pchome.com.tw/f88tw

Avatar_small
شركة التقا للخدمات 说:
2021年3月03日 20:24

A company specializing in the maintenance of home appliances in Riyadh and Jeddah

Avatar_small
تلحيم خزانات البلاست 说:
2021年3月03日 20:25

A company specializing in the maintenance of home appliances in Riyadh and Jeddah

Avatar_small
شركة تنظيف افران 说:
2021年3月03日 20:26

A company specializing in the maintenance of home appliances in Riyadh and Jeddah

Avatar_small
شركة تنظيف خزانات 说:
2021年3月03日 20:27

A company specializing in the maintenance of home appliances in Riyadh and Jeddah

Avatar_small
메이저사이트 说:
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.

Avatar_small
f88tw 说:
2021年4月08日 10:47

敬启者:个人小网站希望大家多多支持 感谢您对我们热心的支持 f88tw┃华歌尔┃I appreciate your kind assistance. f88tw| 粗工| 粗工内容 | 粗工| 粗工内容 |墓园|捡骨流程|捡骨费用|捡骨时间|禁忌|捡骨颜色|捡骨师|新竹|时间|台北|桃园|苗栗|头份|https://mypaper.m.pchome.com.tw/f88tw

Avatar_small
Bong88 说:
2021年6月26日 19:04

Valuable information! Looking forward to seeing your notes posted. Thank you for sharing the nice article. Good to see your article.

Avatar_small
instanthack 说:
2021年7月08日 22:21

I am really impressed with this website, they really can hack instagram tools

Avatar_small
Cialis price in paki 说:
2021年7月09日 07:08

I really enjoyed reading your article. I found this as an informative and interesting post, so I think it is very useful and knowledgeable. I would like to thank you for the effort you have made in writing this article

Avatar_small
우리카지노 说:
2021年7月14日 08:08

Thank you so much for the post you do. I like your post and all you share with us is up to date and quite informative, i would like to bookmark the page so i can come here again to read you, as you have done a wonderful job.

Avatar_small
visit website 说:
2021年7月15日 20:54

I really enjoyed reading your article. I found this as an informative and interesting post, so I think it is very useful and knowledgeable. I would like to thank you for the effort you have made in writing this article

Avatar_small
gwaa 说:
2021年7月16日 20:33

Good website! I truly love how it is easy on my eyes it is. I am wondering how I might be notified whenever a new post has been made. I have subscribed to your RSS which may do the trick? Have a great day!

Avatar_small
สล็อตฟรีเครดิต 说:
2021年7月20日 19:40

Hello! I just wish to give an enormous thumbs up for the nice info you've got right here on this post. I will probably be coming back to your weblog for more soon!

Avatar_small
fire safety 说:
2021年7月20日 23:22

I think this is an informative post and it is very useful and knowledgeable. I really enjoyed reading this post. big fan, thank you!

Avatar_small
iflix 说:
2021年7月23日 06:34

Thanks for a very interesting blog. What else may I get that kind of info written in such a perfect approach? I’ve a undertaking that I am simply now operating on, and I have be Thanks for a very interesting blog.

Avatar_small
Akku Luftpumpe 说:
2021年7月24日 06:43

I understand this column. I realize You put a many of struggle to found this story. I admire your process

Avatar_small
fat burners 说:
2021年7月26日 01:13

I visit your blog regularly and recommend it to all of those who wanted to enhance their knowledge with ease. The style of writing is excellent and also the content is top-notch. Thanks for that shrewdness you provide the readers!

Avatar_small
ambcrypto epaper 说:
2021年7月26日 06:36

Wow, cool post. I’d like to write like this too – taking time and real hard work to make a great article… but I put things off too much and never seem to get started. Thanks though.

Avatar_small
how long does it tak 说:
2021年7月26日 22:09

Pretty nice post. I just stumbled upon your weblog and wanted to say that I have really enjoyed browsing your blog posts. After all I’ll be subscribing to your feed and I hope you write again soon!

Avatar_small
penload 说:
2021年7月27日 07:57

Hello there, I discovered your online web site by way of Google acquire the best for a related topic, your internet site emerged, it appears fantastic. We have saved it during my google social bookmarks.

Avatar_small
canadá eta 说:
2021年7月28日 08:50

Thanks for the blog filled with so many information. Stopping by your blog helped me to get what I was looking for. Now my task has become as easy as ABC

Avatar_small
nextbet 说:
2021年7月29日 05:35

you have done a great job. I will definitely dig it and personally recommend to my friends. I am confident they will be benefited from this site

Avatar_small
Travel Tourism 说:
2021年7月31日 16:36

Thanks for sharing this amazing website

Avatar_small
www.cacamart.com 说:
2021年7月31日 21:56

I am able to tell them I am aware of what they're doing. I would absolutely suggest them on your next upkeep!

Avatar_small
情趣娃娃 说:
2021年7月31日 23:27

Great Article it its really informative and innovative keep us posted with new updates. its was really valuable. thanks a lot.

Avatar_small
amazon sale of phenq 说:
2021年8月01日 04:46

Hey, this day is too much good for me, since this time I am reading this enormous informative article here at my home. Thanks a lot for massive hard work.

Avatar_small
hotmail login 说:
2021年8月02日 08:02

Hello I am so delighted I located your blog, I really located you by mistake, while I was watching on google for something else, Anyways I am here now and could just like to say thank for a tremendous post and a all round entertaining website. Please do keep up the great work.

Avatar_small
khatrimaza 说:
2021年8月02日 11:11

This is helpful, nonetheless it can be crucial so that you can check out the following website

Avatar_small
relx煙彈 说:
2021年8月04日 10:09

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.

Avatar_small
relx煙彈 说:
2021年8月04日 10:18

Appreciate it for this post, I am a big big fan of this website would like to proceed updated.

Avatar_small
free 3d 说:
2021年8月06日 13:55

I think this is an informative post and it is very useful and knowledgeable. therefore, I would like to thank you for the efforts you have made in writing this article.

Avatar_small
加拿大签证 说:
2021年8月06日 19:01

I needed to thank you for this incredible read!! I unquestionably adored each and every piece of it. I have you bookmarked your site to look at the new stuff you post

Avatar_small
pawn shop closest 说:
2021年8月07日 20:12

The article looks magnificent, but it would be beneficial if you can share more about the suchlike subjects in the future. Keep posting.

Avatar_small
site 说:
2021年8月09日 17:41

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.

Avatar_small
mis webmail 说:
2021年8月10日 13:35

Wow! Such an amazing and helpful post this is. I really really love it. It's so good and so awesome. I am just amazed. I hope that you continue to do your work like this in the future also

Avatar_small
instantviews 说:
2021年8月10日 22:47

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.

Avatar_small
instagram takipçi sa 说:
2021年8月11日 11:12

Excellent article. Very interesting to read. I really love to read such a nice article. Thanks! keep rocking

Avatar_small
sig fig converter 说:
2021年8月11日 12:41

you're so interesting! i don't suppose i've read through something like this before. so good

Avatar_small
indian visa 说:
2021年8月11日 17:53

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.

Avatar_small
amherstnational 说:
2021年8月13日 23:56

I’m going to read this. I’ll be sure to come back. thanks for sharing. and also This article gives the light in which we can observe the reality. this is very nice one and gives indepth information. thanks for this nice article...

Avatar_small
visa du lịch ấn độ 说:
2021年8月15日 17:14

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.

Avatar_small
blackjack 说:
2021年8月16日 20:44

There is noticeably big money to comprehend this. I suppose you have made particular nice points in functions also.

Avatar_small
Visa du lịch New Zea 说:
2021年8月16日 23:43

I’m going to read this. I’ll be sure to come back. thanks for sharing. and also This article gives the light in which we can observe the reality. this is very nice one and gives indepth information. thanks for this nice article...

Avatar_small
best home goods 说:
2021年8月18日 04:56

Really a great addition. I have read this marvelous post. Thanks for sharing information about it. I really like that. Thanks so lot for your convene.

Avatar_small
אשרת קנדה 说:
2021年8月24日 10:03

I’m going to read this. I’ll be sure to come back. thanks for sharing. and also This article gives the light in which we can observe the reality. this is very nice one and gives indepth information. thanks for this nice article...

Avatar_small
cctv 说:
2021年8月25日 13:40

Wow! Thank you! I permanently needed to write on my website something like that. Can I implement a portion of your post to my site

Avatar_small
indian visa 说:
2021年8月26日 03:25

Really a great addition. I have read this marvelous post. Thanks for sharing information about it. I really like that. Thanks so lot for your convene.

Avatar_small
188bet 说:
2021年8月26日 08:29

There’s an adage in marketing, that if your target market is everybody, then nobody is going to be your customer. It’s easier to form waves during a pond than in an ocean For More Information.

Avatar_small
đơn xin thị thực Ấn 说:
2021年8月28日 01:12

Wow! Thank you! I permanently needed to write on my website something like that. Can I implement a portion of your post to my site

Avatar_small
خصم 说:
2021年8月28日 18:01

When I am surfing the web occasionally I have discovered a website this really is particularly thought to invoke such as this one. I wanted to share that I found the content on your website has been highly interesting and I learned new things. I will be sure to look for your upcoming post. Many thanks for this fantastic write-up I will come again soon.

Avatar_small
mega888 official doa 说:
2021年8月29日 01:12

You should mainly superior together with well-performing material, which means that see it:

Avatar_small
אשרת קנדה 说:
2021年8月29日 04:56

Wow! Thank you! I permanently needed to write on my website something like that. Can I implement a portion of your post to my site

Avatar_small
mega888 apk 说:
2021年8月30日 01:39

I wrote about a similar issue, I give you the link to my site.

Avatar_small
soaptoday 说:
2021年8月30日 21:00

Thank you because you have been willing to share information with us. we will always appreciate all you have done here because I know you are very concerned with our.

Avatar_small
soap2day movies 说:
2021年8月31日 18:40

Positive site, where did u come up with the information on this posting? I'm pleased I discovered it though, ill be checking back soon to find out what additional posts you include.

Avatar_small
merchant sales rep 说:
2021年8月31日 23:57

Wow! Thank you! I permanently needed to write on my website something like that. Can I implement a portion of your post to my site

Avatar_small
this 说:
2021年9月01日 22:47

All the contents you mentioned in post is too good and can be very useful. I will keep it in mind, thanks for sharing the information keep updating, looking forward for more posts.Thanks

Avatar_small
voyance gratuite 说:
2021年9月03日 20:01

There are a lot of blogs over the Internet. But I can surely say that your blog is amazing in all. It has all the qualities that a perfect blog should have. <a href="https://voyance-telephone-gaia.com">voyance gratuite</a>

Avatar_small
WALTERY PU 说:
2021年9月09日 14:33

Interesting blog I must say, the facts and information written here are accurate.

Avatar_small
Satta king 说:
2021年9月13日 00:11

Gives you the best website address I know there alone you'll find how easy it is.

Avatar_small
HDPE 说:
2021年9月15日 17:37

Thanks for Nice and Informative Post. This article is really contains lot more information about This Topic.

Avatar_small
elizabeth rabbit 说:
2021年9月19日 09:09

Thank you again for all the knowledge you distribute,Good post. I was very interested in the article, it's quite inspiring I should admit. I like visiting you site since I always come across interesting articles like this one.Great Job, I greatly appreciate that.Do Keep sharing! Regards

Avatar_small
FMWhatsapp APK 说:
2021年9月20日 00:19

Nothing compares to Weber using Genesis sort of grills. That is certainly the type gas grills which have been that will always make sure that they deliver a high performance part of down interval FMWhatsapp APK

Avatar_small
FB88 说:
2021年9月24日 12:26

Interesting blog I must say, the facts and information written here are accurate.

Avatar_small
merchant sales rep 说:
2021年9月25日 06:44

An interesting dialogue is price comment. I feel that it is best to write more on this matter, it may not be a taboo topic however usually individuals are not enough to talk on such topics. To the next. Cheers.

Avatar_small
slot 说:
2021年9月25日 21:45

Actually I read it yesterday but I had some thoughts about it and today I wanted to read it again because it is very well written.

Avatar_small
afdah 说:
2021年9月28日 01:09

I visit your blog regularly and recommend it to all of those who wanted to enhance their knowledge with ease. The style of writing is excellent and also the content is top-notch. Thanks for that shrewdness you provide the readers!

Avatar_small
tshirts for nerds 说:
2021年9月28日 17:54

An interesting dialogue is price comment. I feel that it is best to write more on this matter, it may not be a taboo topic however usually individuals are not enough to talk on such topics. To the next. Cheers.

Avatar_small
Stage Curaçao 说:
2021年9月29日 09:20

This is realla y nice and informative, containing all information and also has a great impact on the new technology .

Avatar_small
Stage op Curaçao 说:
2021年10月01日 00:26

I really enjoyed reading your article. I found this as an informative and interesting post, so I think it is very useful and knowledgeable. I would like to thank you for the effort you have made in writing this article

Avatar_small
here 说:
2021年10月02日 01:10

I think this is an informative post and it is very beneficial and knowledgeable. Therefore, I would like to thank you for the endeavors that you have made in writing this article. All the content is absolutely well-researched. Thanks...

Avatar_small
white label 说:
2021年10月03日 12:25

I all around need to uncover to you that I am new to weblog and totally respected this blog page. Reliable I'm going to bookmark your blog . All of you around have mind blowing stories. Offers of interest familiarizing with us your blog.

Avatar_small
ฉีดฟิลเลอร์คาง 说:
2021年10月04日 08:57

I all around need to uncover to you that I am new to weblog and totally respected this blog page. Reliable I'm going to bookmark your blog . All of you around have mind blowing stories. Offers of interest familiarizing with us your blog.

Avatar_small
Ativador Windows 10 说:
2021年10月13日 16:36

I delight in, result in I found exactly what I was having a look for. You have ended my four day long hunt! God Bless you man. Have a great day

Avatar_small
دانلود فیلم سریال 说:
2021年10月27日 01:14

I really like your writing style, great information, thankyou for posting.

Avatar_small
Best Social Plan 说:
2021年10月31日 08:30

Positive site, where did u come up with the information on this posting? I'm pleased I discovered it though, ill be checking back soon to find out what additional posts you include.

Avatar_small
rove haze cart 说:
2021年11月10日 19:16

Hello There. I found your blog using msn. This is an extremely well written article. I will be sure to bookmark it and return to read more of your useful information. Thanks for the post. I’ll certainly comeback <a href="https://rovecartsonline.com">rove haze cart</a>

Avatar_small
seoprovider 说:
2021年11月10日 19:16

thanks for the tips and information..i really appreciate it..

Avatar_small
finance 说:
2021年11月10日 20:24

Really a great addition. I have read this marvelous post. Thanks for sharing information about it. I really like that. Thanks so lot for your convene.

Avatar_small
frugal 说:
2021年11月11日 01:24

I’ve been searching for some decent stuff on the subject and haven't had any luck up until this point, You just got a new biggest fan!..

Avatar_small
free marketing tools 说:
2021年11月11日 21:36

Really a great addition. I have read this marvelous post. Thanks for sharing information about it. I really like that. Thanks so lot for your convene.

Avatar_small
JUNK PICK UP HOUSTON 说:
2021年11月12日 18:16

Really a great addition. I have read this marvelous post. Thanks for sharing information about it. I really like that. Thanks so lot for your convene.

Avatar_small
seo ranking web 说:
2021年11月12日 20:01

very interesting post.this is my first time visit here.i found so mmany interesting stuff in your blog especially its discussion..thanks for the post

Avatar_small
poşet imalatı 说:
2021年11月14日 00:34

Really a great addition. I have read this marvelous post. Thanks for sharing information about it. I really like that. Thanks so lot for your convene.

Avatar_small
fit 说:
2021年11月18日 23:03

Really a great addition. I have read this marvelous post. Thanks for sharing information about it. I really like that.

Avatar_small
รีวิวเว็บufabet 说:
2022年1月22日 21:06

Thank you very much for this useful article. I like it.

Avatar_small
Canada Visa Applicat 说:
2022年1月30日 01:48

very interesting post.this is my first time visit here.i found so mmany interesting stuff in your blog especially its discussion..thanks for the post

Avatar_small
INDIA VISA APPLICATI 说:
2022年2月04日 08:41

Really a great addition. I have read this marvelous post. Thanks for sharing information about it. I really like that. Thanks so lot for your convene.

Avatar_small
Driveway Paving 说:
2022年2月17日 19:49

Superbly written article, if only all bloggers offered the same content as you, the internet would be a far better place.

Avatar_small
dishwasher in uae 说:
2022年3月13日 20:21

Finding the Best Dishwasher in UAE can make cleaning dishware and cutlery a breeze. Doing this review to make it easy for you to get the dishwasher of your dreams in the UAE.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter
Host by is-Programmer.com | Power by Chito 1.3.3 beta | © 2007 LinuxGem | Design by Matthew "Agent Spork" McGee