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?

desertnaut
  • 57,590
  • 26
  • 140
  • 166
NinjaPixels
  • 17
  • 1
  • 7

2 Answers2

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.

slipz
  • 64
  • 4
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.

desertnaut
  • 57,590
  • 26
  • 140
  • 166