Currently:
def get_inet_ip():
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(('mysite.com', 80))
return s.getsockname()[0]
This was based on:
Finding local IP addresses using stdlib Python
However, this looks a bit dubious. As far as I can tell, it opens a socket for mysite.com:80, and then returns the first address for that socket, considering it an IPv4 address. It seems a little dodgy ... I don't think we can ever guarantee that it is.
This is my first question, is it safe? On a server that supports IPv6, can an IPv6 address ever return unexpectedly?
My second question is: how do I get an IPv6 address in a similar way. I am going to change the function to take the optional ipv6 parameter.
source
share