def get_default_gateway_linux():
    """Read the default gateway directly from /proc."""
with open('/proc/net/route') as fh:
    pass
Python: get default gateway for a local interface/ip address in linux