Are a WSGI server and HTTP server required to serve a Flask app?

前端 未结 2 1641
谎友^
谎友^ 2020-11-21 05:47

Setting up Flask with uWSGI and Nginx is quite difficult, and even with buildout scripts it takes quite some time, and has to be recorded to instructions to be reproduced la

2条回答
  •  情书的邮戳
    2020-11-21 05:59

    Presumably you already have a Flask app object and routes set up, but if you create the app like this:

    import flask
    
    app = flask.Flask(__name__)
    

    then set up your @app.route()s, and then when you want to start the app:

    import gevent
    
    app_server = gevent.wsgi.WSGIServer((host, port), app)
    app_server.serve_forever()
    

    Then you can just run your application directly rather than having to tell gunicorn or uWSGI or anything else to run it for you.

    I had a case where I wanted the utility of flask to build a web application (a REST API service) and found the inability to compose flask with other non-flask, non-web-service elements a problem. I eventually found gevent.wsgi.WSGIServer and it was just what I needed. After the call to app_server.serve_forever(), you can call app_server.stop() when your application wants to exit.

    In my deployment, my application is listening on localhost: using flask and gevent, and then I have nginx reverse-proxying HTTPS requests on another port and forwarding them to my flask service on localhost.

提交回复
热议问题