[英]How to correctly end tornado requests?
I'm using Tornado as an API for a basic request to send an email. 我正在使用Tornado作为用于发送电子邮件的基本请求的API。 Depending on the sending result, I'd like to finish the request accordingly.
根据发送结果,我想相应地完成请求。
This is what I've done: 这是我所做的:
def get_routes(tornado_config):
return [
(r"/send", EmailHandler, tornado_config)
]
This is inside EmailHandler
, result is the return of sending email: 这是在
EmailHandler
内部,结果是发送电子邮件的返回:
if result:
self.set_status(200)
self.finish(json.dumps({"status":"ok", "result":result}))
return ''
else:
self.set_status(500)
self.finish(json.dumps({"status": "error", "result":result }))
return ''
The problem is that self.set_status (500)
doesn't seem to add the 500 header. 问题是
self.set_status (500)
似乎没有添加500标头。
Other solution would be: 其他解决方案是:
if result:
return "ok"
else:
return "Message not sent"
But this doesn't respect any standard, as it gets returned at a json {"status":"success", "data":false}
or something like that, even when the sending of email fails. 但这并不符合任何标准,因为即使在发送电子邮件失败时,它也会以json
{"status":"success", "data":false}
或类似的名称返回。
Simply write
: 只需
write
:
self.write({"status":"ok", "result":result})
instead of self.finish
. 而不是
self.finish
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.