Skip to content
Related Articles

Related Articles

Improve Article

Display Hostname and IP address in Python

  • Difficulty Level : Basic
  • Last Updated : 07 Dec, 2017

There are many ways to find hostname and IP address of a local machine. Here is a simple method to find hostname and IP address using python code.
Library used – socket: This module provides access to the BSD socket interface. It is available on all modern Unix systems, Windows, MacOS, and probably additional platforms.

Method used :

  • gethostname() : The gethostname function retrieves the standard host name for the local computer.
  • gethostbyname() : The gethostbyname function retrieves host information corresponding to a host name from a host database.

# Python3 code to display hostname and
# IP address
# Importing socket library
import socket
# Function to display hostname and
# IP address
def get_Host_name_IP():
        host_name = socket.gethostname()
        host_ip = socket.gethostbyname(host_name)
        print("Hostname :  ",host_name)
        print("IP : ",host_ip)
        print("Unable to get Hostname and IP")
# Driver code
get_Host_name_IP() #Function call
#This code is conributed by "Sharad_Bhardwaj".


Hostname :   pppContainer
IP :

NOTE : Output varies machine to machine.

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up
Recommended Articles
Page :