


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使用了动态参数
下面是为任何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的Flask框架中配置多个子域名相关文章请关注PHP中文网!

To maximize the efficiency of learning Python in a limited time, you can use Python's datetime, time, and schedule modules. 1. The datetime module is used to record and plan learning time. 2. The time module helps to set study and rest time. 3. The schedule module automatically arranges weekly learning tasks.

Python excels in gaming and GUI development. 1) Game development uses Pygame, providing drawing, audio and other functions, which are suitable for creating 2D games. 2) GUI development can choose Tkinter or PyQt. Tkinter is simple and easy to use, PyQt has rich functions and is suitable for professional development.

Python is suitable for data science, web development and automation tasks, while C is suitable for system programming, game development and embedded systems. Python is known for its simplicity and powerful ecosystem, while C is known for its high performance and underlying control capabilities.

You can learn basic programming concepts and skills of Python within 2 hours. 1. Learn variables and data types, 2. Master control flow (conditional statements and loops), 3. Understand the definition and use of functions, 4. Quickly get started with Python programming through simple examples and code snippets.

Python is widely used in the fields of web development, data science, machine learning, automation and scripting. 1) In web development, Django and Flask frameworks simplify the development process. 2) In the fields of data science and machine learning, NumPy, Pandas, Scikit-learn and TensorFlow libraries provide strong support. 3) In terms of automation and scripting, Python is suitable for tasks such as automated testing and system management.

You can learn the basics of Python within two hours. 1. Learn variables and data types, 2. Master control structures such as if statements and loops, 3. Understand the definition and use of functions. These will help you start writing simple Python programs.

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Linux new version
SublimeText3 Linux latest version

Atom editor mac version download
The most popular open source editor

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Mac version
God-level code editing software (SublimeText3)