code: def get_gateway_addr():
      
      try:
          import netifaces
          return netifaces.gateways()["default"][netifaces.AF_INET][0]
      except ImportError:
          shell_command = 'netstat -rn'
          if os.name == "posix":
              pattern = \
                  re.compile('(?:default|0\.0\.0\.0|::/0)\s+([\w\.:]+)\s+.*UG')
          elif os.name == "nt":
              if platform.version().startswith("6.1"):
                  pattern = re.compile(".*?0.0.0.0[ ]+0.0.0.0[ ]+(.*?)[ ]+?.*?\n")
              else:
                  pattern = re.compile(".*?Default Gateway:[ ]+(.*?)\n")
          system_out = os.popen(shell_command, 'r').read()
          if not system_out:
              raise NATPMPNetworkError(NATPMP_GATEWAY_CANNOT_FIND,
                                       error_str(NATPMP_GATEWAY_CANNOT_FIND))
          match = pattern.search(system_out)
          if not match:
              raise NATPMPNetworkError(NATPMP_GATEWAY_CANNOT_FIND,
                                       error_str(NATPMP_GATEWAY_CANNOT_FIND))
          addr = match.groups()[0].strip()
          return addr
docs: Use netifaces to get the gateway address, if we can't import it then
         fall back to a hack to obtain the current gateway automatically, since
         Python has no interface to sysctl().
  
         This may or may not be the gateway we should be contacting.
         It does not guarantee correct results.
  
         This function requires the presence of netstat on the path on POSIX
      and NT.