0

I've seen answers to this that include opening a connection to some source that doesn't need to be active. That seems like a waste of time and resources.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770

1 Answers1

0

This seems to be the easiest way to get the IP associated with the default route.

import socket    
hostname = socket.gethostname()
IPAddr = socket.gethostbyname(hostname)
DnsIPAddr = socket.gethostbyaddr(hostname)

print("Your Computer Name is:" + hostname)
print("Your Computer IP Address is:" + IPAddr)
print("According to DNS, your Computer IP Address is:" + DnsIPAddr)