Display Hostname and IP address in Python
We are using the same code which is explained above. Here we compile the hostname and IP address in one program.
Python3
# Importing socket library import socket # Function to display hostname and # IP address def get_Host_name_IP(): try : host_name = socket.gethostname() host_ip = socket.gethostbyname(host_name) print ( "Hostname : " , host_name) print ( "IP : " , host_ip) except : print ( "Unable to get Hostname and IP" ) # Driver code get_Host_name_IP() # Function call # This code is contributed by "Sharad_Bhardwaj". |
Output:
Hostname : pppContainer IP : 10.98.162.168 NOTE: Output varies from machine to machine.
Display Hostname and IP address in Python
There are many ways to find the hostname and IP address of a local machine. Here is a simple method to find the 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 in Python.