xxxxxxxxxx
import socket
# Get the local IP address
def get_local_ip():
try:
# Create a socket object
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# Connect to a server (not actually sending any data)
sock.connect(("8.8.8.8", 80))
# Get the local IP address
local_ip = sock.getsockname()[0]
return local_ip
finally:
sock.close()
# Call the function to get the local IP address
local_ip = get_local_ip()
print("Local IP address:", local_ip)