0

To get the local fixed IP address, I use:

IP_ADDRESS = socket.gethostbyname(socket.getfqdn())

When I run this command on a DHCP client host, it returns '127.0.1.1'.

I know how to get it by parsing the result of ifconfig, but I would prefer to get it directly from python. Does anyone know how to get it?

albar
  • 3,020
  • 1
  • 14
  • 27
  • Thanks, I found an answer that works in both DHCP and non DHCP host: `[(s.connect(('8.8.8.8', 53)), s.getsockname()[0], s.close()) for s in [socket.socket(socket.AF_INET, socket.SOCK_DGRAM)]][0][1]`. – albar Jun 26 '17 at 10:49
  • Take a look at https://stackoverflow.com/questions/166506/finding-local-ip-addresses-using-pythons-stdlib The problem will be that you can have multiple interfaces - the second answer provides you the address of the first interface with internet access (here: checking Google's free DNS server). – TheMeanMan Jun 26 '17 at 10:45

0 Answers0