官网
http://www.ttlsa.com/docs/tornado/#_4
安装
pip3 install tornado
设置请求头
请求头最好设置utf-8,否则容易出中文转码错误
def set_default_header(self):
# 后面的*可以换成ip地址,意为允许访问的地址
self.set_header('Access-Control-Allow-Origin', '*')
self.set_header('Access-Control-Allow-Headers', 'x-requested-with')
self.set_header('Access-Control-Allow-Methods', 'POST, GET, PUT, DELETE')
self.set_header("Content-Type", "application/json;charset=UTF-8")
从url传参
class UrlHandler(RequestHandler):
"""从url传参"""
def get(self, story_id):
self.write("You requested the story " + story_id)
效果
从param传参
class ParamHandler(RequestHandler):
"""从param传参"""
def get(self):
arr = self.get_argument("arr")
if arr:
print('arr = ', arr, type(arr))
arr = eval(arr) # 字符转列表
print('arr = ', arr, type(arr))
self.write("You requested the story " + arr)
效果
从body传参
class GetArrHandle(RequestHandler):
"""从body传参"""
def get(self):
data = json.loads(self.request.body)
project_id = data['project_id']
if project_id:
print('project_id = ', project_id, type(project_id))
result = {
"code": 0 if project_id >= 0 else 1,
"msg": f"执行完成,project_id:{project_id}"
}
self.write(json.dumps(result, ensure_ascii=False))
效果