def echo_json_response(handler,code,status=None,results=None):
"""Takes a JSON package and returns it to the user w/ full HTTP headers"""
if handler is None or code is None:
return False
if status is None:
status = httplib.responses[code]
if results is None:
results = {}
json_res = {'code': code, 'status': status, 'results' : results}
json_response = json.dumps(json_res)
if isinstance(handler, BaseHTTPRequestHandler):
handler.send_response(code)
handler.send_header('Content-Type', 'application/json')
handler.end_headers()
handler.wfile.write(json_response)
return True
elif isinstance(handler, tornado.web.RequestHandler):
handler.set_status(code)
handler.set_header('Content-Type', 'application/json')
handler.write(json_response)
return True
else:
return False
评论列表
文章目录