debugging python请求GET返回HTTP 204

gev0vcfq  于 2022-11-14  发布在  Python
关注(0)|答案(3)|浏览(169)

我不能把我的大脑围绕这个问题:
当我在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中遇到过这种情况。
我做错了什么吗?我不知道。提前感谢您的关注。

5vf7fwbs

5vf7fwbs1#

通过添加以下循环解决:

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

gudnpqoy

gudnpqoy2#

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]>

谢谢

bwntbbo3

bwntbbo33#

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

相关问题