python/flask 应用程序中的端口管理
port management in python/flask application
我正在使用微框架 Flask 和 python 编程语言编写 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:当你为你的服务设置生产环境时——你不需要处理烧瓶代码中的端口- 您只需要在 Web 服务器配置中指定所需的端口,该端口将通过 wsgi layer.
与您的脚本一起使用
我正在使用微框架 Flask 和 python 编程语言编写 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:当你为你的服务设置生产环境时——你不需要处理烧瓶代码中的端口- 您只需要在 Web 服务器配置中指定所需的端口,该端口将通过 wsgi layer.
与您的脚本一起使用