3

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?

2

1 Answer 1

7

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)

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

4 Comments

That gets my computer IP, I want to get like the IP address that if I google "What's my IP?" for example
That's your public ip, you can't get it without any service. Read this: stackoverflow.com/questions/2311510/…
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.
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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.