Home > database >  Keep on getting 127.0.0.1 as my IP address instead of 176.191.77.130 with Python socket
Keep on getting 127.0.0.1 as my IP address instead of 176.191.77.130 with Python socket

Time:06-23

So I've been trying to get my computer's IP address with Python socket programming. Here is my code:

import socket
print(socket.gethostbyname(socket.gethostname())

However, the output is always '127.0.0.1' instead of the actual IP, which is 176.191.77.130

For your knowledge, I have MacOS. What can be the problem here, guys?

Thanks in advance!

CodePudding user response:

So the solution for my specific problem that I've found was to simply use an IP-determining API within my code. I reckon, for many people's code it ought to work as well, since in many cases you just intend to get a public IP of a computer, with which task an API copes really well.

  • Related