How to get your local ipv4 address using python

PHOTO EMBED

Fri Jan 10 2025 16:52:08 GMT+0000 (Coordinated Universal Time)

Saved by @freepythoncode ##python #coding #python

import socket

def get_ipv4_address():
    try:
        hostname = socket.gethostname()
        local_ip = socket.gethostbyname(hostname)
        return local_ip
    except Exception as e:
        return f'Error {e}'
    
print(f'My IPv4 Address is : {get_ipv4_address()}')
content_copyCOPY