gethostbyname
has been obsolete since getaddrinfo
was introduced more than a decade ago. However neither of them are intended to be used to find the IP address of the host your software is running on.
In order to find the IP address of the host your software is running on, the simplest approach is to create a UDP socket and connect it to the remote address you want to communicate with.
import socket
s = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM, 0)
s.connect(('2001:db8::', 53))
print s.getsockname()
On a host with multiple IP addresses this may produce different result depending on which remote address you specify in the code.