python和javascript是轮子王语言,容易上手出机会,造轮子先不想,好好用起来,tornado开发代码量介于bottle和django之间,简单易用,异步特征,引入web和ioloop就可以用,安装依赖
pip install bokeh -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host=mirrors.aliyun.com --upgrade
main代码:
# -*- coding:utf8 -
import tornado.web
import tornado.ioloop
import tornado.gen
from concurrent.futures import ThreadPoolExecutor
import tornado.concurrent
import json
import re
class BaseHandler(tornado.web.RequestHandler):
def set_default_headers(self):
self.set_header('Access-Control-Allow-Origin', '*')
self.set_header('Access-Control-Allow-Headers', '*')
self.set_header('Access-Control-Max-Age', 1000)
self.set_header('Content-type', 'application/json')
self.set_header('Access-Control-Allow-Methods', 'POST, GET, OPTIONS')
self.set_header('Access-Control-Allow-Headers','Content-Type, Access-Control-Allow-Origin, Access-Control-Allow-Headers, X-Requested-By, Access-Control-Allow-Methods')
def options(self):
# 返回方法1
self.set_status(20000) # 这里的状态码一定要设置200,建议
self.finish()
class IndexHandler(BaseHandler):
executor=ThreadPoolExecutor(1000)
@tornado.gen.coroutine
def get(self, *args, **kwargs):
yield self.index_response()
@tornado.concurrent.run_on_executor
def index_response(self,*args,**kwargs):
print (*args)
foods_json=[{
"code": 20000,
"menu": [
{
"path": "/home/main",
"name": "main",
"label": "首页",
"icon": "s-home",
"url": "home/index"
},
{
"path": "/home/mall",
"name": "mall",
"label": "商品管理",
"icon": "video-play",
"url": "mall/index"
},
{
"path": "/home/user",
"name": "user",
"label": "用户管理",
"icon": "user",
"url": "User/index"
},
{
"label": "其他",
"icon": "location",
"children": [
{
"path": "/page1",
"name": "page1",
"label": "页面1",
"icon": "setting",
"url": "Other/PageOne.vue"
},
{
"path": "/page2",
"name": "page2",
"label": "页面2",
"icon": "setting",
"url": "Other/PageTwo.vue"
}
]
}
],
"token": "radlfjdslfdfdfdsfdsf",
"message": "获取成功"
}];
foods=json.dumps(foods_json,ensure_ascii=False)
#self.set_header("Access-Control-Allow-Origin","*")
#self.set_header('Access-Control-Allow-Methods', 'POST, GET, OPTIONS')
#self.set_header("Access-Control-Allow-Headers","*,Content-Type")
#self.set_header("Content-Type","application/json; charset=UTF-8")
self.write(foods)
application = tornado.web.Application([
('/api/menu/getMenu', IndexHandler),
])
if __name__ == "__main__":
application.listen(9999)
tornado.ioloop.IOLoop.instance().start()
运行后结果:
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)