Skip to content

Instantly share code, notes, and snippets.

View giampaolo's full-sized avatar

Giampaolo Rodola giampaolo

View GitHub Profile
@giampaolo
giampaolo / is_connection_err.py
Created March 16, 2023 18:34
Check whether an exception is a connection error
"""
Blog post:
https://gmpy.dev/blog/2023/recognize-connection-errors
"""
import errno, socket, ssl
# Network errors, usually related to DHCP or wpa_supplicant (Wi-Fi).
NETWORK_ERRNOS = frozenset((
errno.ENETUNREACH, # "Network is unreachable"
"""
People erroneously think that any function registered via atexit.register()
will be executed on interpreter exit.
This is not always true. For example, in case of SIGTERM:
import atexit, os, signal
@atexit.register
def cleanup():
print("on exit") # XXX this never gets printed