Python Program to get the IP Address of your Computer

Python Program to get the IP Address of your Computer

Our website provided core java programs examples with output aid beginners and expert coders to test their knowledge gap and learn accordingly.

In this tutorial, we’ll learn how to construct a Python program to acquire our computer’s IP address.

Before we begin the program, let us learn more about the IP Address.

IP Address:

An IP address (internet protocol address) is a numerical representation that identifies a specific network interface.

IPv4 addresses are 32 bits long. There are a total of 4,294,967,296 (232) unique addresses available. IPv6 addresses are 128 bits long, allowing for a total of 3.4 x 1038 (2128) distinct addresses.

Various reserved addresses and other considerations restrict the overall useable address pool in both versions.

IP addresses are binary numbers, but they’re usually written in decimal (IPv4) or hexadecimal (IPv6) to make them easier to understand and use for people.

The Internet Protocol (IP) is a set of standards and specifications for generating and transferring data packets, or datagrams, across networks. The Internet Protocol (IP) is a component of the Internet protocol suite’s Internet layer. IP is considered part of the network layer in the OSI paradigm. IP is usually used in conjunction with a higher-level protocol, the most common of which being TCP. RFC 791 is the standard that governs the IP protocol.

There are two ways to define an IP address. There are two types of IP addresses: IPv4 and IPv6. IP Address is defined as a 32-bit number in IPv4. An IP Address is defined as a 128-bit number in IPv6.

172.15.254.1 is an example of an IPv4 address.

IPv6 address example: 2000:0db8:85a3:0000:0000:8a2e:0370:7334

In this tutorial, we will use the socket library to get our computer’s IP address.

To obtain our computer’s IP address, we will utilize the socket library method gethostbyname(). It accepts a hostname as an argument and returns the host’s IPv4 address.

Program to get the IP Address of your Computer in Python

Approach:

  • To utilize the method gethostbyname() in the socket library, first import the socket module using the import statement.
  • To obtain the IP address of the host, we must give hostname as an argument to gethostbyname (). So, let’s acquire our computer’s hostname using the gethostname() method and send it as an argument to gethostbyname() to get the IP address.
  • Also, assign the variable the value provided by the gethostbyname() method.
  • Print the IP address of the computer.

Below is the implementation:

# To utilize the method gethostbyname() in the socket library,
# first import the socket module using the import statement.
import socket
# To obtain the IP address of the host,
# we must give hostname as an argument to gethostbyname ().
# So, let's acquire our computer's hostname using the gethostname() method
# and send it as an argument to gethostbyname() to get the IP address.
# Also, assign the variable the value provided by the gethostbyname() method.

compIpAdress = socket.gethostbyname(socket.gethostname())
# Print the IP address of the computer.
print("The IP Address of this computer is [", compIpAdress, ']')

Output:

The IP Address of this computer is [ 10.98.152.178 ]

Related Programs: