在我的代码中,我提出了一些发布请求。如何在该调用中捕获连接拒绝错误? 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
提交
取消