Discovering local IP addresses in python.

Tech Notes

There are lots of methods people use to find the local machines ip address from python. All of them returned weird psuedo loopback addresses in my situation. However this worked amazingly well. It requires avahi-daemon installed and running (Bonjour/Rendezvous) aptitude install avahi-daemon python snippet hostname = socket.gethostname() address = socket.gethostbyname("%s.local" % hostname) addr = address, port print addr Here are some that didn't work. #ip_addr = socket.getaddrinfo(socket.gethostname(), None)[0][4][0] #ip_addr = socket.gethostbyaddr(socket.gethostname()) #print([ip for ip in socket.gethostbyname_ex(socket.gethostname())] #from netifaces import interfaces, ifaddresses, AF_INET #for ifaceName in interfaces(): #addresses = [i['addr'] for i in ifaddresses(ifaceName)[AF_INET]] #print '%s: %s' % (ifaceName, ', '.join(addresses))