python socket public ip code example
Example 1: python socket get client ip address
import socket
hostname = socket.gethostname()
ip_address = socket.gethostbyname(hostname)
print(f"Hostname: {hostname}")
print(f"IP Address: {ip_address}")
Example 2: python socket get client ip address
1. Import the socket module.
2. Get the hostname using the socket.gethostname() method and store it in a variable.
3. Find the IP address by passing the hostname as an argument to the
socket.gethostbyname() method and store it in a variable.
4. Print the IP address.
Example 3: python socket get client ip address
Hostname: DESKTOP-A0PM5GD
IP Address: 192.168.43.15