From 96e5db4ede950a4bbdf3380e2f2765161371fd13 Mon Sep 17 00:00:00 2001 From: Akshada-26 <130067077+Akshada-26@users.noreply.github.com> Date: Mon, 23 Oct 2023 17:17:05 +0530 Subject: [PATCH] IP address and hostname --- Python/IP address and hostname | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 Python/IP address and hostname diff --git a/Python/IP address and hostname b/Python/IP address and hostname new file mode 100644 index 00000000..65d5bef8 --- /dev/null +++ b/Python/IP address and hostname @@ -0,0 +1,18 @@ +#Akshada-26 + +import socket + +def get_ip_address_and_hostname(): + # Get the hostname of the computer + hostname = socket.gethostname() + + try: + # Get the IP address associated with the hostname + ip_address = socket.gethostbyname(hostname) + print(f"Hostname: {hostname}") + print(f"IP Address: {ip_address}") + except socket.gaierror: + print("Couldn't get the IP address for the hostname.") + +if __name__ == "__main__": + get_ip_address_and_hostname()