flask和tornado的区别

flask:

from flask import Flask,request
import cv2,face_recognition,re,uuid,pymysql
from flask_sqlalchemy import SQLAlchemy

命名

app = Flask(__name__)

调试模式

app.debug = True

配置数据库

app.config["SQLALCHEMY_DATABASE_URI"]="mysql://root:123@127.0.0.1:3306/face"
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
db = SQLAlchemy(app)

创建表(同Django)
class Face(db.model):
  表名
  __tablename__ = "face"
  字段

配置路由,flask路由基于装饰器
@app.route('/',methods=['GET','POST'])
def index():
  if request.method == "POST":
    res = request.get_data()



tornado:
import tornado.ioloop
import tornado.web
定义一个类处理请求
class main(RequestHandler):
  def get(self):
    发送数据
    self.write('111111')
    返回页面
    self.rander('main.html')


配置路由
def make_app():
  return tornado.web.Application([
    (r"/",main)

  ])



app = make_app()
监听端口
app.listen(8888)
启动web程序,开始监听端口的连接
tornado.ioloop.IOLoop.current().start()

settings配置
settings = {

'template_path':'view',
}

app = Application([(r'/',Index)],**settings,debug=True)
启动
if __name__ == '__main__':
http_server = HTTPServer(app)
http_server.bind(8888)
http_server.start(1)

IOLoop.current().start()





























原文地址:https://www.cnblogs.com/xuezhihao/p/11410846.html