为什么用flas连接两个应用时出现404错误

2024-10-02 12:37:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试用flask连接两个应用程序:

  @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)  

问题是,当我发送一个邮递员代理应用程序它不工作,它显示在第二个应用程序(前置)404

当我一行一行地跑进去

   @app.route('/prepository/', methods = ["GET","POST"])

SyntaxError: unexpected EOF while parsing

我不知道这两个问题是否相关。你知道吗

编辑

现在我尝试过任何突然出现在我脑海中的东西,我发现如果我把帖子直接发到prepository,它确实有效。 假设这两个应用程序之间的连接有问题。 我还更改了用于get和post的库,它是flask.request.get_json 现在是requests.postrequests.get 还是不行。你知道吗


Tags: runjsonapp应用程序datagetifrequest
1条回答
网友
1楼 · 发布于 2024-10-02 12:37:16

我不确定这是如何工作的,看起来您的prepositoryagent路由都配置为处理GETPOST请求,但是您的路由不区分传入的GET和POST请求。默认情况下,如果没有指定路由上支持的方法,flask将默认为支持GET请求。但是,如果不检查传入请求,您的路由就不知道如何处理传入请求,因为GETPOST都受支持。一个简单的条件如下: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响应来验证配置的正确性,因为很难判断数据对象是否按原样正确设置。然后,在验证两个服务都正常工作之后,就可以开始构建应用程序服务,以便彼此通信。你知道吗

希望这有帮助!你知道吗

相关问题 更多 >

    热门问题