我正在尝试使用烧瓶连接两个应用程序:
@app.route("/agent/", methods = ['POST', 'GET'])
def agent():
data = request.get_json(force = True)
if(data):
if(format(data['option']) == "1"):
print(data['prepository']['run'])
requests.post('http://some ip:4001/prepopsitory/', data['prepository'])
return "hi"
app.run(host = 'some ip', port = 4998)
还有这个
app = Flask(__name__)
@app.route('/prepository/', methods = ["GET","POST"])
def recibe():
data = request.get_json(force = True)
if(data):
run = data['prepository']['run']
prepository.formatea(run,1)
return "hi"
app.run(host = 'some ip', port = 4001)
问题是当我将 Postman 发送到 Agent 应用程序时它不起作用,它在第二个应用程序(存储库)上显示 404
当我一行一行地运行时
@app.route('/prepository/', methods = ["GET","POST"])
说
SyntaxError:解析时出现意外的 EOF
不知道这两个问题有没有关系。
编辑
现在我已经尝试了任何突然出现在我脑海中的东西,我发现如果我将帖子直接发送到 prepository 它确实有效。鉴于我认为这两个应用程序之间的连接存在问题。此外,我更改了用于获取和发布的库,它flask.request.get_json
现在是requests.post
或requests.get
仍然不起作用。
我不确定这是如何工作的,看起来您的prepository
和agent
路由都已配置为处理GET
和POST
请求,但您的路由不区分传入的 GET 和 POST 请求。默认情况下,如果您没有在路由上指定支持的方法,flask 将默认支持 GET 请求。但是,如果不检查传入请求,您的路由不知道如何处理传入请求,因为GET
和POST
都受支持。一个简单的条件,如下所示:if flask.request.method == 'POST':
可以用来区分这两种类型的请求。也许您可以添加上面提到的条件检查,以检查每种类型的请求,以便您的应用程序服务可以适当地响应。类似的东西:
@app.route('/agent', methods=['POST', 'GET'])
def agent():
if request.method == "GET":
msg = "GET Request from agent route"
return jsonify({"msg":msg})
else:
# Handle POST Request
data = request.get_json()
if data:
# handle data as appropriate
msg = "POST Request from agent route handled"
return jsonify({"msg": msg})
app.run(host = 'some ip', port = 4998)
出于调试目的,只需发送一个非常简单的json
响应来验证配置的正确性,因为很难判断您的数据对象是否按原样正确设置。然后,一旦您的两个服务都被验证为正常工作,您就可以开始构建您的应用程序服务以相互通信。
希望这有帮助!
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句