在我的代碼中,我提出了一些發布請求。如何在該調用中捕獲連接拒絕錯誤? try: headers = {'content-type': 'application/x-www-form-urlencoded; charset=UTF-8'} response = requests.request("POST", local_wallet_api + "v1/wallet/get_public_keys", headers=headers) res = json.loads(response.text) except Exception as e: if e.errno == errno.ECONNREFUSED: print("connection refused") sys.exit(141)我已經嘗試了上面的代碼,但它不起作用,因為它說e沒有errno參數。有沒有正確的方法來處理這種錯誤?
3 回答

慕森王
TA貢獻1777條經驗 獲得超3個贊
from requests.exceptions import ConnectionError
try:
headers = {'content-type': 'application/x-www-form-urlencoded; charset=UTF-8'}
response = requests.request("POST", local_wallet_api + "v1/wallet/get_public_keys", headers=headers)
res = json.loads(response.text)
except ConnectionError:
sys.exit(141)
添加回答
舉報
0/150
提交
取消