前言
flask 启动访问平常开发的时候可以用命令行flask run
运行,正式部署到线上环境不会这样用。
Gunicorn 环境准备
Web框架致力于如何生成HTML代码,而Web服务器用于处理和响应HTTP请求。Web框架和Web服务器之间的通信,需要一套双方都遵守的接口协议。WSGI协议就是用来统一这两者的接口的
常用的 WSGI 容器有 Gunicorn 和 uWSGI,用 Gunicorn 可以直接命令启动,不需要编写配置文件,相对uWSGI要容易很多,这里选择用Gunicorn作为容器。
Gunicorn 是一个Python WSGI HTTP服务器, 只支持在Unix/Linux环境下运行。
项目结构与代码
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello World!'
@app.route('/hello')
def hello():
return render_template('hello.html')
if __name__ == '__main__':
app.run()
hello.html内容
<!DOCTYPE html>
<html lang="en">
&