2

I'm having PHP and Python scripts running on different machines, and I want to get the IP of the machine from these scripts.

It seems like it's directing me to the loopback device and not to the network card whenever I try this from within the machine (which is where these scripts are located). When I called PHP script that only runs phpinfo(); from the browser I had the wanted result but when I used curl it gave me the loopback address.

I tried multiple ways that should work

in Python: socket.gethostbyname(socket.gethostname())

in PHP: $_SERVER['HTTP_HOST'] or getHostByName(php_uname('n'));

The bash command hostname -I, is working for me since it's ignoring the loopback address. I wanted to know if there's something like this built-in PHP/Python. Otherwise, I think my solution would be to just call this bash command from these scripts.

Daniel
  • 1,895
  • 9
  • 20

1 Answers1

0

For python : socket.gethostbyname(socket.gethostname()) returns values contained in /etc/hosts. I guess you have 127.0.0.1 localhost

(check Finding local IP addresses using Python's stdlib)

If you want to get the ip directly on your Nic take a look at this thread, you can use external lib : How can I get the IP address from NIC in Python?

Charly Roch
  • 368
  • 1
  • 4
  • 15