Finding HostName and IP address using Python Program.

Avatar

 BY TEJAS




Python is a very Important Language in today's computer world.


In this example, we see how to write a Python program which shows our IP address and our HostName. This Example is very little in size almost 4-5 lines of code.


In this example, we are using inbuilt functions in python which shows our IP and HostName.
So, let's take a turn to our example of HostName and IP address using Python Program.

For this example, we are using Python 3.8.1 stable version.

Host Name & IP Address

In this example, we are using the "sockets" Python library which helps to find our IP and Hostname.
Socket library helps to create simple servers and clients for communicate with sockets.

First, we install the sockets Python library by using pip install sockets command typed in our python terminal. After installing Sockets we are importing the socket in our program by using import socket just like other libraries.



Also Check:-





Then we use the inbuilt function called a socket.gethostname() which gets our HostName and then it stores in the hostname variable which is created by us. Similarly, for IP address We use a socket.gethostname(hostname) function. Which stores in ip_address variable. And then we print our IP and HostName on-screen using print function.


Thank You.



below here is a Python code.

demo.py :-
    

import socket 
hostname = socket.gethostname() 
ip_address = socket.gethostbyname(hostname) 
print(f"Your HostName is: {hostname}") 
print(f"Your IP Address is: {ip_address}"

    


    

import socket 
hostname = socket.gethostname() 
ip_address = socket.gethostbyname(hostname) 
print(f"Your HostName is: {hostname}") 
print(f"Your IP Address is: {ip_address}"

    


Search
Advertisement
Share With
Follow us on
Advertisement
Advertisement