【问题标题】:GET request working from POSTMAN failed while using python使用 python 时来自 POSTMAN 的 GET 请求失败
【发布时间】:2019-11-19 11:42:44
【问题描述】:

当我使用邮递员检查状态时,我正在尝试使用 get 请求获取声纳扫描状态。我得到了预期的输出。但是当我尝试使用 python3.6.3 时。它通过以下异常

Traceback (most recent call last):
 File "C:\Python37\lib\site-packages\urllib3\response.py", line 331, in 
_error_catcher
   yield
 File "C:\Python37\lib\site-packages\urllib3\response.py", line 413, in read
    data = self._fp.read(amt)
 File "C:\Python37\Lib\http\client.py", line 447, in read
    n = self.readinto(b)
 File "C:\Python37\Lib\http\client.py", line 491, in readinto
    n = self.fp.readinto(b)
 File "C:\Python37\Lib\socket.py", line 589, in readinto
    return self._sock.recv_into(b)
 File "C:\Python37\Lib\ssl.py", line 1049, in recv_into
    return self.read(nbytes, buffer)
 File "C:\Python37\Lib\ssl.py", line 908, in read
    return self._sslobj.read(len, buffer)
ConnectionResetError: [WinError 10054] An existing connection was 
 forcibly closed by the remote host

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
   File "C:\Python37\lib\site-packages\requests\models.py", line 749, in generate
for chunk in self.raw.stream(chunk_size, decode_content=True):
   File "C:\Python37\lib\site-packages\urllib3\response.py", line 465, in stream
data = self.read(amt=amt, decode_content=decode_content)
  File "C:\Python37\lib\site-packages\urllib3\response.py", line 430, in read
raise IncompleteRead(self._fp_bytes_read, self.length_remaining)
  File "C:\Python37\Lib\contextlib.py", line 130, in __exit__
self.gen.throw(type, value, traceback)
  File "C:\Python37\lib\site-packages\urllib3\response.py", line 349, in _error_catcher
raise ProtocolError('Connection broken: %r' % e, e)
urllib3.exceptions.ProtocolError: ("Connection broken: ConnectionResetError(10054, 'An existing connection was forcibly closed by the remote host', None, 10054, None)", ConnectionResetError(10054, 'An existing connection was forcibly closed by the remote host', None, 10054, None))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:/Users/vjannapureddy/Documents/testing/testingsonarapi.py", line 11, in <module>
   response = requests.request("GET", url, headers=headers, params=querystring)
   File "C:\Python37\lib\site-packages\requests\api.py", line 58, in request
return session.request(method=method, url=url, **kwargs)
  File "C:\Python37\lib\site-packages\requests\sessions.py", line 512, in request
   resp = self.send(prep, **send_kwargs)
   File "C:\Python37\lib\site-packages\requests\sessions.py", line 662, in send
r.content
   File "C:\Python37\lib\site-packages\requests\models.py", line 827, in content
self._content = b''.join(self.iter_content(CONTENT_CHUNK_SIZE)) or b''
   File "C:\Python37\lib\site-packages\requests\models.py", line 752, in generate
raise ChunkedEncodingError(e)
requests.exceptions.ChunkedEncodingError: ("Connection broken: 
 ConnectionResetError(10054, 'An existing connection was forcibly closed by the remote host', None, 10054, None)", ConnectionResetError(10054, 'An existing connection was forcibly closed by the remote host', None, 10054, None))

我尝试使用 python 3.7 运行,但它不起作用,然后我尝试在 linux 环境中使用 python3.6.3,但它不起作用。

我们使用的是 SonarQube 企业版,它具有 MFA 安全验证。

import requests

url = "https://xxxsonarqube.xx.com/api/qualitygates/project_status"

querystring = {"analysisId" : "AWu4CtcA08Z7GjqNo7V6"}

headers = {
    'Authorization': "Basic xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
    }

response = requests.request("GET", url, headers=headers, 
params=querystring)

print(response.text)
print(response.status_code)

这是预期的输出,并且在使用 postman 时有效。

{
    "projectStatus": {
        "status": "ERROR",
        "conditions": [
            {
                "status": "OK",
                "metricKey": "bugs",
                "comparator": "GT",
                "errorThreshold": "0",
                "actualValue": "0"
            },
            {
                "status": "OK",
                "metricKey": "vulnerabilities",
                "comparator": "GT",
                "errorThreshold": "0",
                "actualValue": "0"
            },
            {
                "status": "ERROR",
                "metricKey": "code_smells",
                "comparator": "GT",
                "errorThreshold": "0",
                "actualValue": "72"
            }
        ],
        "periods": [],
        "ignoredConditions": false
    }
}

【问题讨论】:

  • 似乎使用 python 2.7 工作,但不适用于 python 3.6 及更高版本。但不幸的是,我不能使用 Python 2.7。有没有其他解决方案?

标签: python python-requests sonarqube postman


【解决方案1】:

你试过用谷歌搜索错误吗? 似乎错误是这样的:

python: [Errno 10054] An existing connection was forcibly closed by the remote host

所以它是一个服务器到客户端的错误,你的 python 脚本似乎没问题。

【讨论】:

  • 如果这是一个客户端错误,我的邮递员应该会失败,但我一直在发送大约 10 到 15 次垃圾邮件,而且它每次都能在另一边运行,python 每次运行时都会给我相同的结果.
  • 似乎使用 python 2.7 工作,但不适用于 python 3.6 及更高版本。但不幸的是,我不能使用 Python 2.7。
猜你喜欢
  • 2021-10-29
  • 1970-01-01
  • 1970-01-01
  • 2021-11-05
  • 2013-01-15
  • 1970-01-01
  • 2019-01-09
  • 1970-01-01
  • 2018-06-05
相关资源
最近更新 更多