Python的Flask框架中配置多个子域名的方法讲解

yipeiwu_com6年前Python基础

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服务等),并做好泛域名解析,然后再进行调试。当然使用公网域名和服务器来调试也未尝不可。

PS:
1.如果某个blueprint默认就需要实用a.domain.com,那么在定义blueprint时候:

a=Blueprint(‘a' ,__name__,subdomain='a')

这样,该bp下面的所有url routing走的都是a.domain.com/xxx

2.在某个具体的url routing定义时,如果需要实用a.domain.com,那么这么写:

@www.route(‘/hello',methods=['GET','POST'],subdomain='a')
def xxx():
…..

3.我在具体实践中,默认的routing都是走的www,这是在__init__.py中:

app.url_map.default_subdomain='www'

其实就是设置默认子域名,这样默认不做设置的话,路由走的就是www。
那这个时候如果访问domain.com,即不带www的话,就会报404了,怎么办呢,我是在nginx层面解决这个问题的,在nginx.conf增加一个server:

server {
server_name domain.com;
rewrite ^(.*) http://www.domain.com$1 permanent;
}

相关文章

Python安装pycurl失败的解决方法

Centos安装pycurl centos 安装pycurl yum install python-devel curl-devel pip3 install pycurl Mac...

python用户评论标签匹配的解决方法

python用户评论标签匹配的解决方法

我们观察用户评论发现:属性词往往和情感词伴随出现,原因是用户通常会在描述属性时表达情感,属性是情感表达的对象。还发现:属性词和专用情感词基本都是名词或形容词(形谓词)。 算法流程图如下:...

使用pip发布Python程序的方法步骤

使用pip发布Python程序的方法步骤

写过 Python 程序的小伙伴们都知道,需要 import 个非 Python 自带的软件包时,都要用到 pip 这个程序。平时我们都是用 pip,如果我们写好了一个程序,想让大家都能...

pytorch自定义二值化网络层方式

任务要求: 自定义一个层主要是定义该层的实现函数,只需要重载Function的forward和backward函数即可,如下: import torch from torch.aut...

基于python(urlparse)模板的使用方法总结

一、简介 urlparse模块用户将url解析为6个组件,并以元组形式返回,返回的6个部分,分别是:scheme(协议)、netloc(网络位置)、path(路径)、params(路径段...