将另一个站点添加到同一服务器上的my flask应用程序

2024-10-02 22:24:47 发布

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

因此,我目前有sitea.com设置,并且已经积极运行了一年多。现在我想添加siteb.comsiteb.com也会有一个博客,名为siteb.com/blog

有人能帮我吗

以下是我当前的配置: app.py

@app.route('/')
def home():
        '''
        home() will show the homepage of my website sitea.com
        '''
        return render_template('index.html')

问题是sitea.comsiteb.com都指向同一个服务器,所以如果我尝试访问每一个服务器,它将仅将其拉到现有的sitea.com站点,但我希望通过flask使其基于域名

我该怎么做?我假设需要对flask配置中现有的sitea.com进行更改,同时添加另一个siteb.com更改

有人能帮我吗

谢谢


Tags: thepy服务器comappflaskhomedef
1条回答
网友
1楼 · 发布于 2024-10-02 22:24:47

一种可能的情况是,您可以分割站点url并在不同的域中呈现。比如:

@app.route('/')
      def home():
         # Return different results depending on the host
         url = split_url(request.url)
         if "www.sitea.com" == url['domain']:
             return "<h1>You are visiting sitea.com</h1><p>Here's your URL " +
     request.url + "</p>"
         else:
             return "<h1>You are not visiting www.sitea.com</h1><p>Here's your URL "
     + request.url + "</p>"

    def split_url(url):
         "Returns the full URL in two parts, the domain, and the path"
         url = url.split('/', 3)
         return {'domain': url[2], 'path': url[3]}

相关问题 更多 >