簡體   English   中英

使用python時來自POSTMAN的GET請求失敗

[英]GET request working from POSTMAN failed while using python

當我使用郵遞員檢查狀態時,我嘗試使用get request獲取聲納掃描狀態。 我得到預期的輸出。 但是當我嘗試使用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運行,然后在linux3.6.3不能運行的Linux環境中嘗試運行。

我們正在使用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)

這是預期的輸出,可以在使用郵遞員時使用。

{
    "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:[Errno 10054]現有連接被遠程主機強行關閉

因此,它是服務器端到客戶端的錯誤,您的python腳本似乎還可以。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM