如何让一个Flask应用程序在两个不同的端口上侦听?

问题描述 投票:5回答:2

是否可以在两个不同的端口上安装一个带有路由的烧瓶应用程序?我的Flask应用程序需要监听webhooks,由于某些安全性,它无法在默认端口上接收外部POST请求。可以这样做吗?

@app.route('/hook/<sourcename>', methods=["POST"], port=5051)
def handle_hook(sourcename):
  print 'asdf'
python flask
2个回答
2
投票

默认情况下,服务器仅侦听单个端口。它是否更有意义,因为附加端口需要额外的功能,在本地代理POST请求的第二个端口上实现前端服务器?有许多记录良好的方法来做这个such as this one


3
投票

如果您在C插件中不需要任何套接字代码,那么gevent可以提供帮助,例如:同

import gevent
from gevent.pywsgi import WSGIServer

app = Flask(__name__)

https_server = WSGIServer((HOST, HTTPS_PORT), app, keyfile=PRIVKEY, certfile=CERT)
https_server.start()

http_server = WSGIServer((HOST, HTTP_PORT), app)
http_server.start()

while True:
    gevent.sleep(60)
© www.soinside.com 2019 - 2024. All rights reserved.