python/flask应用程序中的端口管理 [英] port management in python/flask application

查看:152
本文介绍了python/flask应用程序中的端口管理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有Python编程语言的微型框架Flask编写REST API.在调试模式下,应用程序将检测源代码中的任何更改,并使用相同的主机和端口重新启动自身.在生产模式(无调试)下,应用程序在更改源代码时不会自行重新启动它,因此我必须自己重新启动应用程序;在这种情况下的问题是,应用程序无法使用以前在旧版本的应用程序中使用的端口运行,因此,每次更新应用程序时,我都被要求更改端口:

这是我的代码的样子:

from flask import Flask, jsonify, request
import json
import os

app = Flask(__name__) 


@app.route('/method1', methods=['GET'])
def method1():
    return jsonify({"result":"true"})

@app.route('/method2', methods=['GET'])
def method2():
    return jsonify({"result":"true"})


if __name__ == '__main__':
    app.run(debug=True,port=15000)

如何解决此问题?还是每次应用程序更新都必须更改端口?

解决方案

此代码test.py不会更改.run() args中指定的端口:

from flask import Flask    

app = Flask(__name__)

@app.route("/")
def index():
    return "123"

app.run(host="0.0.0.0", port=8080) # or host=127.0.0.1, depends on your needs

如果您在run函数中指定了所需的端口,则没有什么可以迫使flask绑定到允许范围内的另一个TCP端口.如果此端口已被其他应用使用-您将看到 OSError: [Errno 98] Address already in use 发射后.

UPD :如果我多次使用python test.py命令运行此代码,则会从我的电脑中输出以下内容:

artem@artem:~/Development/$ python test.py
 * Running on http://0.0.0.0:8080/ (Press CTRL+C to quit)
^Cartem@artem:~/Development$ python test.py
 * Running on http://0.0.0.0:8080/ (Press CTRL+C to quit)
^Cartem@artem:~/Development/$ python test.py
 * Running on http://0.0.0.0:8080/ (Press CTRL+C to quit)
^Cartem@artem:~/Development/$ python test.py
 * Running on http://0.0.0.0:8080/ (Press CTRL+C to quit)
127.0.0.1 - - [20/Nov/2017 17:04:56] "GET / HTTP/1.1" 200 -

如您所见,flask每次都绑定到8080端口.

UPD2 :在为您的服务设置生产环境时-您不需要照顾烧瓶代码中的端口-您只需要指定所需的通过 wsgi层与您的脚本一起使用的Web服务器配置中的端口.. >

I am writing a REST API using the micro framework Flask with python programming language. In the debug mode the application detect any change in source code and restart itself using the same host and port. In the production mode (no debug) the application does not restart it self when changing the source code so I have to restart the application by my self; the problem in this case is that the application cannot run using a port previously used in an old version the application, so I am asked to change the port with every app update:

This is how my code look like:

from flask import Flask, jsonify, request
import json
import os

app = Flask(__name__) 


@app.route('/method1', methods=['GET'])
def method1():
    return jsonify({"result":"true"})

@app.route('/method2', methods=['GET'])
def method2():
    return jsonify({"result":"true"})


if __name__ == '__main__':
    app.run(debug=True,port=15000)

How to solve this issue? or do I have to change port with every application update?

解决方案

This code test.py doesn't change port that's specified in .run() args:

from flask import Flask    

app = Flask(__name__)

@app.route("/")
def index():
    return "123"

app.run(host="0.0.0.0", port=8080) # or host=127.0.0.1, depends on your needs

There is nothing that can force flask to bind to another TCP port in allowed range if you specified the desired port in run function. If this port is already used by another app - you will see OSError: [Errno 98] Address already in use after launching.

UPD: This is output from my pc if I run this code several time using python test.py command:

artem@artem:~/Development/$ python test.py
 * Running on http://0.0.0.0:8080/ (Press CTRL+C to quit)
^Cartem@artem:~/Development$ python test.py
 * Running on http://0.0.0.0:8080/ (Press CTRL+C to quit)
^Cartem@artem:~/Development/$ python test.py
 * Running on http://0.0.0.0:8080/ (Press CTRL+C to quit)
^Cartem@artem:~/Development/$ python test.py
 * Running on http://0.0.0.0:8080/ (Press CTRL+C to quit)
127.0.0.1 - - [20/Nov/2017 17:04:56] "GET / HTTP/1.1" 200 -

As you can see flask gets binded to 8080 port every time.

UPD2: when you will setup production env for your service - you won't need to take care of ports in flask code - you will just need to specify desired port in web server config that will work with your scripts through wsgi layer.

这篇关于python/flask应用程序中的端口管理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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