<p><a href="https://flask.palletsprojects.com/tutorial/deploy/" rel="noreferrer">The official tutorial discusses deploying an app to production.</a>一个选项是使用Waitress,一个生产WSGI服务器。其他服务器包括Gunicorn和uWSGI。</p>
<blockquote>
<p>When running publicly rather than in development, you should not use
the built-in development server (<code>flask run</code>). The development server
is provided by Werkzeug for convenience, but is not designed to be
particularly efficient, stable, or secure.</p>
<p>Instead, use a production WSGI server. For example, to use Waitress,
first install it in the virtual environment:</p>
<pre class="lang-none prettyprint-override"><code>$ pip install waitress
</code></pre>
<p>You need to tell Waitress about your application, but it doesn’t use
<code>FLASK_APP</code> like flask run does. You need to tell it to import and call
the application factory to get an application object.</p>
<pre class="lang-none prettyprint-override"><code>$ waitress-serve --call 'flaskr:create_app'
Serving on http://0.0.0.0:8080
</code></pre>
</blockquote>
<p>或者可以在代码中使用<code>waitress.serve()</code>,而不是使用CLI命令。</p>
<pre class="lang-py prettyprint-override"><code>from flask import Flask
app = Flask(__name__)
@app.route("/")
def index():
return "<h1>Hello!</h1>"
if __name__ == "__main__":
from waitress import serve
serve(app, host="0.0.0.0", port=8080)
</code></pre>
<pre><code>$ python hello.py
</code></pre>