Python requests GET 返回 HTTP 204

3

我无法理解这个问题:

当我在我的IDE(Pycharm)或通过命令行运行此代码时,我得到一个204的HTTP响应,但没有内容。当我在调试器中设置断点以查看发生了什么时,代码执行良好,并且r.contentr.text填充了请求结果。当在调试器中运行时,r.status_code的值也为200

代码:

    r = requests.post(self.dispatchurl, verify=False, auth=HTTPBasicAuth(self.user, self.passwd))
    print 'first request to get sid: status {}'.format(r.status_code)
    json_data = json.loads(r.text)
    self.sid = json_data['sid']
    print 'the sid is: {}'.format(self.sid)
    self.getresulturl = '{}/services/search/jobs/{}/results{}'.format(self.url, self.sid, self.outputmode)
    x = requests.get(self.getresulturl, verify=False, auth=HTTPBasicAuth(self.user, self.passwd))
    print 'second request to get the data: status {}'.format(x.status_code)
    print 'content: {}'.format(x.text)

通过调试器运行时的输出:

first request to get sid: status 201
the sid is: sanitizedatahere
second request to get the data: status 200
content: {"preview":false...} 

Process finished with exit code 0

当我正常执行代码而不使用调试器时,第二个响应返回 204
输出结果:
first request to get sid: status 201
the sid is: sanitizedatahere
second request to get the data: status 204
content: 

Process finished with exit code 0

我猜测这可能与调试器减缓请求并允许服务器响应数据有关?这似乎是一种竞争条件。我从未在使用requests时遇到过这种情况。
我是否做错了什么?我很无助。提前感谢您的帮助。

如果是这种情况,请使用time.sleep(10)进行测试。您还可以检查此帖子中描述的详细调试输出,以查看是否有差异。https://dev59.com/4mkv5IYBdhLWcg3wdQdT?rq=1 - NotARobot
3个回答

0
在这种情况下,在生成SID后,当状态响应为200但dispatchState不是DONE时,直接尝试获取结果。因此,在检查结果响应时,它会返回204。
我们可以通过过滤来持续检查作业的状态(“<s:key name="dispatchState">DONE</s:key>”)。因此,一旦dispatchState显示为DONE,就可以开始检查结果,然后响应代码将直接给出200。

0

HTTP 204无内容成功状态响应代码表示请求已成功,但客户端无需离开当前页面。默认情况下,204响应是可缓存的。 以下设置将解决此问题。

r = requests.get(splunk_end, headers=headers, verify=False)
while r.status_code == 204:
time.sleep(1)
r = requests.get(splunk_end, headers=headers, verify=False)

204 响应已转换为 200。请检查下面的日志。

https://localhost:8089/services/search/jobs/4D44-A45E-7BDB8F0BE473/results?output_mode=json
/usr/lib/python2.7/site-packages/botocore/vendored/requests/packages/urllib3/connectionpool.py:768: InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.org/en/latest/security.html
  InsecureRequestWarning)
/usr/lib/python2.7/site-packages/botocore/vendored/requests/packages/urllib3/connectionpool.py:768: InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.org/en/latest/security.html
  InsecureRequestWarning)
<Response [204]>
/usr/lib/python2.7/site-packages/botocore/vendored/requests/packages/urllib3/connectionpool.py:768: InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.org/en/latest/security.html
  InsecureRequestWarning)
<Response [200]>

谢谢


0

通过添加以下循环解决:

    while r.status_code == 204:
        time.sleep(1)
        r = requests.get(self.resulturl, verify=False, auth=HTTPBasicAuth(self.user, self.passwd))

正如我所猜测的那样,Rest API 收集结果需要更长时间,因此返回了 204。当运行调试器时,它会减慢进程,使得 API 能够完成初始请求,从而返回 200

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接