gpt-4o-mini-2024-07-18· OpenAI
WildChat-4.8M (AI2)
write a python code to print IPv4 Address
- User
write a python code to print IPv4 Address
- Assistant
To print an IPv4 address in Python, you can use the built-in
socketlibrary, which provides a way to work with network addresses. Below is a simple Python script that retrieves and prints the local machine's IPv4 address.import socket def get_ipv4_address(): try: # Get the hostname of the local machine hostname = socket.gethostname() # Get the local machine's IPv4 address using the hostname ipv4_address = socket.gethostbyname(hostname) return ipv4_address except socket.error as e: print(f"Error retrieving IPv4 address: {e}") if __name__ == "__main__": address = get_ipv4_address() print(f"IPv4 Address: {address}")