I'm trying to make a program that gets the IP address that the program is running on, is there a way of getting an IP address without using an API of some sort?
-
Your public IP or your private IP?Carcigenicate– Carcigenicate2020-06-03 20:07:03 +00:00Commented Jun 3, 2020 at 20:07
-
1you will find your answer here stackoverflow.com/questions/166506/…Vignesh Krishnan– Vignesh Krishnan2020-06-03 20:08:05 +00:00Commented Jun 3, 2020 at 20:08
Add a comment
|
1 Answer
For your private ip:
import socket
hostname = socket.gethostname()
IPAddr = socket.gethostbyname(hostname)
print("Your Computer IP Address is:" + IPAddr)
Python's socket module is a great module for "all those networking stuff", like getting IP address.
For public ip you'll need to use an external service. Read more about it: Getting a machine's external IP address with Python
For example you can use: https://pypi.org/project/publicip/ (didn't try it myself)
4 Comments
KB856KDHI7RBE
That gets my computer IP, I want to get like the IP address that if I google "What's my IP?" for example
Roim
That's your public ip, you can't get it without any service. Read this: stackoverflow.com/questions/2311510/…
KB856KDHI7RBE
Yeah I see, it would be weird getting an IP without requesting something. I was wondering if it would be simple or so without requesting a site/api.
peets
finds "127.0.1.1"; the IP stored in /etc/hosts correlated with my computer name (Linux Ubuntu). But I need my LAN IP received from the local DNS.