How to have one Flask app listen on two different ports?

前端 未结 2 1516
清酒与你
清酒与你 2020-12-18 07:01

Is it possible to have a single flask app with routes on two different ports? My Flask app needs to listen for webhooks and due to some security biz it can\'t receive foreig

相关标签:
2条回答
  • 2020-12-18 07:11

    A server by default only listens to a single port. Wouldn't it make more sense, since the additional port requires additional functionality, to implement a front-end server on the second port that proxies the POST request locally? There are many well-documented ways to do this such as this one

    0 讨论(0)
  • 2020-12-18 07:24

    If you don't need any socket code inside C plugins, gevent could help, e.g. with

    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)
    
    0 讨论(0)
提交回复
热议问题