【问题标题】:wait an answer after post request python在发布请求python后等待答案
【发布时间】:2018-01-08 12:22:15
【问题描述】:

我正在开发一个充当桥梁的服务器,我向他发送一个请求,然后我再次将其发送到一个 api, 当我使用 localhost 转换此代码时,它可以工作,我从 api 得到答案。 但是当我用我的服务器打开它时,我没有得到任何响应。 我认为这取决于等待时间,但我将睡眠设置为 10 秒,仍然没有答案。 有什么想法吗?

def do_POST(self):

    print( "incomming http: ", self.path )

    content_length = int(self.headers['Content-Length']) # <--- Gets the size of data
    post_data = self.rfile.read(content_length) # <--- Gets the data itself

    #print(str(post_data))
    print("Got data.")

    response = bytes("Data Received.", "utf-8")
    self.send_response(200)

    self.send_header("Content-Length", str(len(response)))
    self.end_headers()
    self.wfile.write(response) #send response

    test_name = "Jhon Doe"
    respserver = getId(test_name)
    try :
        print("sending to the other server..")
        idname = int(respserver)
        msg = post_data.decode("utf-8")
        js = json.loads(msg)

        respserver = sendIntRR(idname, js)
        print("waiting a response")
        sleep(1)
        print("Api : ", respserver)

    except :
        print("ExternalId : ", test_name)
        print(respserver)

请求从这里发送:

def sendIntRR (id : int, msg : str) -> str:
   url = apiurl + "collect/" + str(id)
   querystring = {"period":"300000","filter":"3","returnList":"True"}

   payload = msg
   headers = {
     'authorization': bearerClient,
     'content-type': "application/json",
     'cache-control': "no-cache",
     }

   response = requests.api.request('post', url, data=payload, headers=headers,params=querystring, json=None, verify=False)

   return(response.text)

我在服务器终端中得到了什么:

sending to the other server..
ExternalId :  Jhon Doe
8

非常感谢,G.B

【问题讨论】:

    标签: python sockets server python-requests


    【解决方案1】:

    我得到的答案是睡眠,一旦我移除睡眠,它就起作用了,不知道如何解释,但现在起作用了

    【讨论】:

      猜你喜欢
      • 2021-11-21
      • 2021-03-28
      • 1970-01-01
      • 2012-11-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多