
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python program to find the IP Address of the client
In this tutorial, we are going to find the IP address of the client using the socket module in Python. Every laptop, mobile, tablet, etc.., have their unique IP address. We will find it by using the socket module. Let's see the steps to find out the IP address of a device.
Algorithm
1. Import the socket module. 2. Get the hostname using the socket.gethostname() method and store it in a variable. 3. Find the IP address by passing the hostname as an argument to the socket.gethostbyname() method and store it in a variable. 4. Print the IP address.
Let's write code for the above algorithm.
Example
## importing socket module import socket ## getting the hostname by socket.gethostname() method hostname = socket.gethostname() ## getting the IP address using socket.gethostbyname() method ip_address = socket.gethostbyname(hostname) ## printing the hostname and ip_address print(f"Hostname: {hostname}") print(f"IP Address: {ip_address}")
Output
If you run the above program, you will get the following output.
Hostname: DESKTOP-A0PM5GD IP Address: 192.168.43.15
Conclusion
If you have any doubts regarding the tutorial, mention them in the comment section.
- Related Articles
- C# program to find IP Address of the client
- Java program to find IP Address of the client
- Python program to Display Hostname and IP address?
- Java program to Get IP address of the system
- Python program to remove leading zeros from an IP address
- C Program to find IP Address, Subnet Mask & Default Gateway
- How can we get the client's IP address in ASP.NET MVC C#?
- C Program to validate an IP address
- JavaScript program to retrieve clients IP address
- Validate IP Address in Python
- Program to find all possible IP address after restoration in C++
- Program to display hostname and IP address C
- C# program to Display Hostname and IP address
- C Program to display hostname and IP address
- Java program to display Hostname and IP address

Advertisements