KB856KDHI7RBE
KB856KDHI7RBE

Reputation: 37

Python | Is there a way to get my IP address?

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?

Upvotes: 3

Views: 6066

Answers (1)

Roim
Roim

Reputation: 3066

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)

Upvotes: 6

Related Questions