Python-如何从同一台客户端计算机运行多个Flask应用 [英] Python - How to run multiple flask apps from same client machine

查看:510
本文介绍了Python-如何从同一台客户端计算机运行多个Flask应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个烧瓶应用程序脚本,如下所示:

I have one flask application script as given below :

from flask import Flask
app = Flask(__name__)

@app.route("/<string:job_id>")
def main(job_id):
    return "Welcome!. This is Flask Test Part 1"

if __name__ == "__main__":
    job_id = 1234
    app.run(host= '0.0.0.0')

我还有另一个烧瓶应用程序脚本,如下所示:

I have another flask application script as below :

from flask import Flask
app = Flask(__name__)

@app.route("/<string:ID>")
def main(ID):
    return "Welcome!. This is Flask Test Part 2"

if __name__ == "__main__":
    ID = 5678
    app.run(host= '0.0.0.0')

两个脚本之间的唯一区别是参数名称及其值.现在,我的问题是假设我正在执行第一个脚本.所以我会得到类似

The only difference between both the scripts is the argument name and its value. Now my question is assume that I am executing the first script. So I will get something like

* Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)

当我在浏览器中执行 http://127.0.0.1:5000/1234 时,我可以看

When I execute http://127.0.0.1:5000/1234 in my browser I am able to see

欢迎!这是烧瓶测试的第1部分"

"Welcome!. This is Flask Test Part 1"

现在此服务器处于活动状态,我正在执行第二个脚本.所以我再次得到

Now with this server active, I am executing the second script. So again I get

* Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)

但是当我在浏览器中执行 http://127.0.0.1:5000/5678 时,我能够看到

But when I execute http://127.0.0.1:5000/5678 in my browser I am able to see

欢迎!这是烧瓶测试的第1部分"

"Welcome!. This is Flask Test Part 1"

代替

欢迎!这是Flask测试第2部分"

"Welcome!. This is Flask Test Part 2"

我不明白我在哪里做错.任何输入或更改都会有所帮助

I don't understand where I am doing mistake. Any inputs or alterations will be helpful

推荐答案

默认情况下,Flask开发服务器在端口5000上侦听,因此当您运行不带端口号的Flask应用程序时,它将在5000上运行.

Flask development server by default listens on port 5000 so when you run a Flask app without port number it will run on 5000.

您可以在同一台机器上运行多个Flask应用程序,但端口号不同.假设您的脚本名称为script1.pyscript2.py:

You can run a number of Flask app on the same machine but with the different port numbers. Let's say your scripts names are script1.py and script2.py:

$ export FLASK_APP=script1.py
$ flask run --host 0.0.0.0 --port 5000

打开一个新终端

$ export FLASK_APP=script2.py
$ flask run --host 0.0.0.0 --port 5001

这篇关于Python-如何从同一台客户端计算机运行多个Flask应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆