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:


  • Top 25 Pattern Programs in C++
    In this article, I will show you the Top 25 Pattern Programs in C++ that you must learn which will help you to understand the usage of nested loops. We will learn to create different geometrical shapes like Squares, triangles, Rhombus, etc. We have provided the code as well as output for all the patterns…
  • Currency Converter in C++
    In this article, we will build a simple program for Currency Converter in C++. The user will be able to convert currencies like Dollar, Euro, Pound, and Rupee. This is a console application and very simple to understand as we only use a bunch of conditionals to write the entire program. Features Users can convert…
  • SQLite | CRUD Operations in Python
    CRUD stands for Create Read Update Delete. I will show you how to perform CRUD Operations in Python. You need basic Tkinter and SQLite knowledge before you read further. This app is straightforward, when you will open this app, a GUI with 4 green colored buttons will open to perform CRUD(create read update delete) operations….
  • Number Guessing Game in C++
    In this article, we will build a simple Number Guessing Game in C++. It’s a game in which the player has to guess a secret number in a range that is generated by the computer and upon a wrong guess by the player, the game will give hints to the player to guess the correct…
  • Image background remover in Python
    Hello friends, in this article, we will learn how to create an Image background remover in Python. I will first show you a simple program and then you will see a smile GUI made using tkinter. We need to install rembg, you can install rembg using the pip command: Image background remover in Python program…


Share:
Avatar of Harry

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