0

I am trying to run a python server on a Raspberry Pi, and I need the local IP address of it to connect. I realize I can just type ifconfig and get my IP address, but I would really like to be able to get it from the python script. I have tried socket.gethostbyname(socket.getfqdn()), but that seems to only work on Windows. When I run it on my raspberry pi, it seems to always return 127.0.1.1. Does anyone know a command that will get the local IP address on linux?

0

2 Answers 2

0

You can use an external package, for example netifaces, or you can look for the ipaddress of a given interface, specified by its name. Take a look at this question.

Sign up to request clarification or add additional context in comments.

Comments

0

The reason why socket.gethostbyname(socket.getfqdn()) returns 127.0.1.1 is because you have a line similar to this in /etc/hosts on your raspberry pi

127.0.1.1 <host_name>

Try changing 127.0.1.1 to the static IP address of your pi and it should return correctly.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.