Python exposes a socket library that allows us to perform networking tasks such as getting the IP Address as well as getting the Hostname of our system.

import socket

  host_name = socket.gethostname()
  host_ip = socket.gethostbyname(host_name)
  print(f"Hostname: {host_name}")
  print(f"IP : {host_ip}")
  print("Could not get the IP Address or Hostname")

The above code imports the socket library and then runs a safe block of code.

We need to first get the hostname and then use that to get the IP Address.