2

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?

1 Answers1

4

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)

Roim
  • 2,986
  • 2
  • 10
  • 25
  • That gets my computer IP, I want to get like the IP address that if I google "What's my IP?" for example – KB856KDHI7RBE Jun 03 '20 at 20:15
  • That's your public ip, you can't get it without any service. Read this: https://stackoverflow.com/questions/2311510/getting-a-machines-external-ip-address-with-python – Roim Jun 03 '20 at 20:17
  • 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. – KB856KDHI7RBE Jun 03 '20 at 20:18
  • 1
    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. – peets Apr 14 '23 at 04:32