Get IP address using Python

Get IP address using Python

An IP address is a label that is used to identify one or more devices on a computer network, such as the internet. And today we will learn how to get an IP address using Python. It is comparable to a postal address. An IP address is a long number written in binary

Today we will learn how to find the IP address of the machine we are working on. We will use the socket module which provides access to the BSD socket interface. This is available on all modern operating systems like Unix systems, Windows, MacOS, and probably additional platforms.



So let’s see and understand the code


Code

# importing socket module
import socket 

# getting machine name
hostname = socket.gethostname() 

# getting IP Address
IPAddr = socket.gethostbyname(hostname) 

# printing hostname
print("Your Computer Name is:" + hostname) 

# printing IP Address
print("Your Computer IP Address is:" + IPAddr) 

Output



Your Computer Name is: LAPTOP-HAHA2FDA
Your Computer IP Address is:192.168.12.324


Run Python Code


Thanks for reading


Keep updated for more content like this


Also read:


  • Most Underrated Database Trick | Life-Saving SQL Command
    Hello folks! Today we are again back with a super important article on the Most underrated SQL & Database Trick to save your entire application. Maintaining the data integrity of your application is very important in this world of software development. However, even experienced engineers may encounter situations where mistakes happen in changing or updating…
  • Python List Methods
    Hello friends, in this article, we will explore various Python List methods, indispensable tools in a programmer’s toolkit for manipulating lists efficiently. List methods in Python are built-in functions designed to perform specific tasks when applied to lists. To access a method in Python, we simply use the dot notation (.), linking the method to…
  • Top 5 Free HTML Resume Templates in 2024 | With Source Code
    Introduction Hello friends! Welcome to another article where I will share more useful resources for free. Today, I will share the Best 5 HTML resume templates in 2024 with source code. I have found the best HTML templates with a combination of different types; one is good in design, and the other is good in…
  • How to See Connected Wi-Fi Passwords in Windows?
    Hello friends! Today we are back with an amazing article on How to See Connected Wi-Fi Passwords in Windows. It happens every time we forget the password of connected WiFi as we do not have to enter the password manually every time. But what if today we show you a way to see the WiFi…
  • 2023 Merry Christmas using Python Turtle
    Introduction Hello folks! Merry Christmas in advance. As Christmas 2023 is around the corner, today in this article we will make Merry Christmas, greeting messages, and other decorations using the Python Turtle. In this article on Merry Christmas using Python Turtle, we will go through the whole code and explain it properly for you to…


Share:

Author: Harry

Hello friends, thanks for visiting my website. I am a Python programmer. I, with some other members, write blogs on this website based on Python and Programming. We are still in the growing phase that's why the website design is not so good and there are many other things that need to be corrected in this website but I hope all these things will happen someday. But, till then we will not stop ourselves from uploading more amazing articles. If you want to join us or have any queries, you can mail me at admin@copyassignment.com Thank you