Python Getting LAN IP Address on Ubuntu

So, I'm trying to get the LAN IP address on the computer on which the program is running, and compare it with the IP addresses passed to it via UDP.

However, when I use:

print str(socket.gethostbyname(socket.gethostname()))

It returns 127.0.0.1, which should be 192.168.1.9.

I looked at the linux machine and got the ip port address of lo (loopBack)? I do not know exactly what it is, but it should get the eth0 IP address.

I found that I can subprocess the bash command "ifconfig eth0", but it returns a large block of string. I can process it to what I need, but it will work 3 times per second on beaglebone, so I would like it to be a little more efficient.

Is there a more elegant way to do this?

Is it possible to just change the gethostname target?

Why is it targeting lo port?

Thanks for the help.

+5
2

netifaces python, .

+1

:

print str(socket.gethostbyname(socket.getfqdn()))

/etc/hosts, , , , 127.0.0.1, socket.gethostbyname() .

, socket.getfqdn() . ifconfig , . .

+4

All Articles