python 请求 GET 返回 HTTP 204

问题描述 投票:0回答:3

我无法思考这个问题:

当我在 IDE (pycharm) 中或通过命令行运行此代码时,我收到

204
HTTP 响应,但没有内容。当我在调试器中设置断点以查看发生了什么时,代码执行良好,并且
r.content
r.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

我是不是做错了什么?我不知所措。预先感谢您的关注。

python debugging python-requests
3个回答
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


0
投票

HTTP 204 No Content 成功状态响应代码表示请求已成功,但客户端不需要离开当前页面。默认情况下,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
投票

本例中,生成一个SID后,当状态响应为200但dispatchState未DONE时直接尝试获取结果。因此,当检查结果响应时,它给出 204。

我们可以通过过滤不断检查作业的状态(“DONE”)。所以一旦dispatchState显示DONE,就去检查结果,那么响应代码将直接给出200。

© www.soinside.com 2019 - 2024. All rights reserved.