我有这个服务器代码,我使用'add_url_rule'来添加一个视图函数。
app = connexion.App(__name__, specification_dir='./swagger/')
app.add_url_rule('/etsiproxy/notify/<vnf_id>',
view_func=handle_sol003_notifications,
methods=['GET', 'POST'])
app.server = 'tornado'
# This is run from a different thread, create a default event loop for
# this thread for tornado.
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
self.threadLoop = loop
# Flask with Tornado works in single thread. Use the thread to store
# the server object, so that it can be accessed from the handler
thr = threading.current_thread()
thr.api_provider = self
app.run(server='tornado', port=self._port, host=self._host, debug=False)
这是我的处理函数:
def handle_sol003_notifications(cls, vnf_id):
notification = connexion.request.get_json()
if connexion.request.method == 'GET':
return None, 204
if notification['notificationType'] == 'VnfLcmOperationOccurrenceNotification':
payload = Sol003Yang.YangInput_Sol003_Sol003Rpc.from_dict(
{"vnfinstanceid": notification['vnfInstanceId'],
"notificationstatus": notification['notificationStatus'],
"operationstate": notification['operationState'],
"operation": notification['operation'],
"vnflcmopoccid": notification['vnfLcmOpOccId'],
})
task = get_api_provider().run_in_tasklet_loop(cls.DTS.query_rpc("I,/sol003:sol003-rpc", 0, payload))
return None, 204
当在这个 url 上点击一个 post 请求时,我会得到这个错误: TypeError: the view function did not return a valid response。这是因为我返回 None,而烧瓶不喜欢这样。
但是如果我使用return '', 204,它会给出另一个错误,说不能用 204 发送正文。
在这种情况下,我怎样才能返回 204 no content?