0
0
PythonProgramBeginner · 2 min read

Python Program to Get IP Address

You can get your computer's IP address in Python using socket.gethostbyname(socket.gethostname()) which returns the IP as a string.
📋

Examples

InputRun the program on a computer connected to the internet
Output192.168.1.10
InputRun the program on a computer with no network connection
Output127.0.0.1
InputRun the program on a computer connected via Wi-Fi
Output192.168.0.5
🧠

How to Think About It

To get the IP address, we ask the computer for its network name using gethostname(), then find the IP linked to that name using gethostbyname(). This gives the IP address as a string.
📐

Algorithm

1
Import the socket module
2
Get the computer's hostname using gethostname()
3
Find the IP address for that hostname using gethostbyname()
4
Print the IP address
💻

Code

python
import socket

ip_address = socket.gethostbyname(socket.gethostname())
print("Your IP address is:", ip_address)
Output
Your IP address is: 192.168.1.10
🔍

Dry Run

Let's trace getting the IP address on a computer with hostname 'my-pc' and IP '192.168.1.10'.

1

Import socket module

The socket module is ready to use.

2

Get hostname

socket.gethostname() returns 'my-pc'

3

Get IP address

socket.gethostbyname('my-pc') returns '192.168.1.10'

4

Print IP address

Output: Your IP address is: 192.168.1.10

StepFunction CallReturned Value
2socket.gethostname()'my-pc'
3socket.gethostbyname('my-pc')'192.168.1.10'
💡

Why This Works

Step 1: Import socket module

We import socket because it has functions to work with network details like IP addresses.

Step 2: Get hostname

The gethostname() function gets the computer's network name, which is needed to find its IP.

Step 3: Get IP address

Using gethostbyname() with the hostname returns the IP address as a string.

Step 4: Print result

We print the IP so the user can see their computer's network address.

🔄

Alternative Approaches

Using socket with a dummy connection
python
import socket

s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(('8.8.8.8', 80))
ip = s.getsockname()[0]
s.close()
print('Your IP address is:', ip)
This method finds the actual IP used for internet connection, better if multiple network interfaces exist.
Using netifaces library
python
import netifaces

iface = netifaces.gateways()['default'][netifaces.AF_INET][1]
ip = netifaces.ifaddresses(iface)[netifaces.AF_INET][0]['addr']
print('Your IP address is:', ip)
Requires installing netifaces package; gives detailed IP info per network interface.

Complexity: O(1) time, O(1) space

Time Complexity

The program runs a few system calls to get the hostname and IP, which are constant time operations.

Space Complexity

Only a few variables store strings, so space used is constant.

Which Approach is Fastest?

The simple socket.gethostbyname approach is fastest and simplest, but the dummy connection method is more accurate for active IP.

ApproachTimeSpaceBest For
socket.gethostbyname(gethostname())O(1)O(1)Quick internal IP
socket dummy connectionO(1)O(1)Active internet IP
netifaces libraryO(1)O(1)Detailed interface info
💡
Use the socket method with a dummy connection to get your active internet IP if your computer has multiple network adapters.
⚠️
Beginners often expect gethostbyname(gethostname()) to always return the public IP, but it may return localhost or internal IP instead.