Web应用中的动态子域处理(Flask)

2024-05-16 12:51:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我将使用flask创建一个web应用程序,应用程序的一部分将涉及一个子域(例如,user1.appname.org)。

我不知道如何在flask配置中动态创建这些子域,或者如何将它们部署到生产服务器。

做这个最好的方法是什么?


Tags: 方法子域org服务器web应用程序flask部署
2条回答

为了补充Sean Viera的post,还需要设置SERVER_NAME config变量。

文档:http://flask.pocoo.org/docs/config/#SERVER_NAME

The name and port number of the server. Required for subdomain support (e.g.: 'myapp.dev:5000') Note that localhost does not support subdomains so setting this to “localhost” does not help. Setting a SERVER_NAME also by default enables URL generation without a request context but with an application context.

要在本地进行测试,需要向hosts文件中添加条目,如下所示:

127.0.0.1       cvshark.local
127.0.0.1       robert.cvshark.local
127.0.0.1       www.cvshark.local

所有Flask的路由构造都支持^{}关键字参数(这包括对路由变量的支持)。

@app.route("/", subdomain="static")
def static_index():
    """Flask supports static subdomains
    This is available at static.your-domain.tld"""
    return "static.your-domain.tld"

@app.route("/dynamic", subdomain="<username>")
def username_index(username):
    """Dynamic subdomains are also supported
    Try going to user1.your-domain.tld/dynamic"""
    return username + ".your-domain.tld"

相关问题 更多 >