title: Reliably get IPV6 Address in Python
body: <p>at the moment I do:</p>
  
  <pre><code>def get_inet_ip():
      s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
      s.connect(('mysite.com', 80))
      return s.getsockname()[0]
  </code></pre>
  
  <p>This was based on:
  <a href="https://stackoverflow.com/questions/166506/finding-local-ip-addresses-using-pythons-stdlib">Finding local IP addresses using Python&#39;s stdlib</a></p>
  
  <p>However, This looks a bit dubious. As far as I can tell, it opens a socket to mysite.com:80, and then returns the first address for that socket, assuming it to be an IPv4 address. This seems a bit dodgy... i dont think we can ever guaranteee that to be the case.</p>
  
  <p>Thats my first question, is it safe? On an IPv6-enable server, could the IPv6 address ever be returned unexpectedly? </p>
  
  <p>My second question, is how do I get the IPv6 address in a similar way. Im going to modify the function to take an optional ipv6 paramater.</p>