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


# 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) 


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

Run Python Code

Thanks for reading

Keep updated for more content like this

Also read:

  • Python 3.12.1 is Now Available
    Thursday, December 7, 2023 Python enthusiasts, rejoice! Python 3.12.1, the first maintenance release of Python 3.12, is now available for download here. This update packs over 400 bugfixes, build enhancements, and documentation changes since 3.12.0. The 3.12 series introduces major features, including: Type annotations see improvements with a new syntax for generic classes (PEP 695)…
  • Best Deepfake Apps and Websites You Can Try for Fun
    Deepfake is one technology that remains consistently impressive. Apps like Lensa AI have taken the internet by storm. It lets you create deepfake portraits within minutes, and the results look incredibly authentic. While deepfake apps are for fun, deepfake videos can be deeply problematic if used for misrepresenting someone, particularly in politics. So in this…
  • Amazon launched free Prompt Engineering course: Enroll Now
    Introduction In this course, you will learn the principles, techniques, and the best practices for designing effective prompts. This course introduces the basics of prompt engineering, and progresses to advanced prompt techniques. You will also learn how to guard against prompt misuse and how to mitigate bias when interacting with FMs. Course objectives In this…
  • 10 GitHub Repositories to Master Machine Learning
    1. ML-For-Beginners by Microsoft2. ML-YouTube-Courses3. Mathematics For Machine Learning4. MIT Deep Learning Book5. Machine Learning ZoomCamp6. Machine Learning Tutorials7. Awesome Machine Learning8. VIP Cheat Sheets for Stanford’s CS 229 Machine Learning9. Machine learning Interview10. Awesome Production Machine Learning
  • Hello World in 35 Programming Languages
    A timeless custom known as the “Hello, World!” program marks the start of every programmer’s adventure in the wide world of programming. This surprisingly easy article examines the various ways that 35 different programming languages express this well-known statement. Every “Hello, World!” implementation offers a glimpse into the distinct syntax and personality of the language…


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 Thank you