Python使用flask获取Post和get请求

@app.route('/company_qa', methods=['POST', 'GET'])
def company_qa_login():
	"""
	公司问答的请求代码
	:return:
	"""
	starttime = datetime.datetime.now()
	if request.method == 'POST':
		#读取form类型数据
		formData = request.form
		formDict = formData.to_dict()
		print(formDict)
		resp = jsonify({'answer': qa(formDict['question'], alice,model,id_question)})
		resp.headers['Access-Control-Allow-Origin'] = '*'
		endtime = datetime.datetime.now()
		print(endtime - starttime)
		return resp
	elif request.method == 'GET':
		print(request.args["question"])
		resp = jsonify({'answer': qa(request.args["question"], alice, model, id_question)})
		resp.headers['Access-Control-Allow-Origin'] = '*'
		endtime = datetime.datetime.now()
		print(endtime - starttime)
		return resp
	else:
		resp = jsonify({'answer': "对不起,您的问题暂时不能回答"})
		resp.headers['Access-Control-Allow-Origin'] = '*'
		return resp

 

你可能感兴趣的