Python Program to Get Hostname and IP Address

This article is created to cover some programs in Python, that find and prints hostname (device name) and IP address (local IP address) of a computer system. Here are the list of programs:

Find and Print IP Address

The question is, write a Python program to get and print IP address of a computer system. Here is its answer:

import socket

hostnm = socket.gethostname()
ipaddr = socket.gethostbyname(hostnm)

print("\nIP Address is:", ipaddr)

Here is its sample output:

python get ip address of computer

Both methods namely gethostname() and gethostbyname() are defined in socket module, therefore we've to import it before using any of its function. Then use the function in a way that, write socket object, followed by dot (.) operator and then function named, like shown in the program given above.

From above program, the following statement:

hostnm = socket.gethostname()

assigns the hostname to the Python variable named hostnm. And the following statement:

ipaddr = socket.gethostbyname(hostnm)

assigns the IP address of hostnm (hostname or device name) to ipaddr, a Python variable. Above program can also be written as:

import socket
print("\nIP Address is:", socket.gethostbyname(socket.gethostname()))

Find and Print Hostname or Device Name

This program get and prints hostname or device name in similar way as done in previous program.

import socket

hn = socket.gethostname()
print("\nHostname or Device Name =", hn)

Here is its sample output:

python program get hostname

If you open About window of your computer system from System Properties, you will see the following information that contains device name exactly same as produced through above program. In my case, the snapshot given below shows some part of About window of my computer system:

python system device name hostname

Get Hostname and IP Address using Function

This program uses two user-defined functions to get and print both Hostname and IP address of computer system. That is, the first function named FindHost() returns hostname, whereas the second function named FindIP() returns IP address.

import socket

def FindHost():
    return socket.gethostname()

def FindIP():
    hn = FindHost()
    ip = socket.gethostbyname(hn)
    return ip

print("\nHostname =", FindHost())
print("IP Address =", FindIP())

Here is its sample output:

python program get ip address

Get Hostname and Local IP Address using Class

This is the last program created using class named fresherearth. This class contains two member functions namely FindHost() and FindIP(). To access member function of a class, an object of same class is required

Therefore I've created an object named obj of class fresherearth. So that using this object, I can access the member function of class using dot (.) operator. The self is a reference to the current object of class.

import socket

class fresherearth:
    def FindHost(self):
        return socket.gethostname()
    def FindIP(self):
        hn = self.FindHost()
        ip = socket.gethostbyname(hn)
        return ip

obj = fresherearth()
print("\nHostname or Device Name =", obj.FindHost())
print("Local IP Address =", obj.FindIP())

Here is its sample output:

python program ip address

Same Program in Other Languages

Python Online Test


« Previous Program Next Program »