In my code, I've made some post requests. How can I catch connection refused error in that call?
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)
I've tried the above code, but it is not working as it says e
has no errno
parameter. Is there any proper way to handle this kind of error?
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)
you can use requests.exceptions.RequestException
as your exception.
Example:
except requests.exceptions.RequestException as e:
# exception here
For the list of requests exceptions, check requests.exception documentation. You can refer to this link.
take a look at this.
you can get the errno by e.args[0].reason.errno
.
also use this except:
except requests.exceptions.ConnectionError as e:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With