Search code examples
pythonipv6

HTTPSConnection fails binding specific ipv6 source address python3


I'am trying to update freedns.afraid.org stuff with my ipv4 and ipv6 addresses, binding the requests also with the respective interfaces's ip; ipv4 is ok, ipv6 isn't. Follows the ipv6 faulty code snippet (ipv4 is simmetric):

...
conn = http.client.HTTPSConnection("freedns.afraid.org", timeout=10,source_address=(ipv6,0))
req = ("/dynamic/update.php?blablabla&address=%s" % ipv6)
conn.request("GET", req, headers=headers)
...

The conn.request returns:

Traceback (most recent call last):
  File "./test2.py", line 39, in <module>
    conn.request("GET", req, headers=headers)
  File "/usr/lib64/python3.4/http/client.py", line 1137, in request
    self._send_request(method, url, body, headers)
  File "/usr/lib64/python3.4/http/client.py", line 1182, in _send_request
    self.endheaders(body)
  File "/usr/lib64/python3.4/http/client.py", line 1133, in endheaders
    self._send_output(message_body)
  File "/usr/lib64/python3.4/http/client.py", line 963, in _send_output
    self.send(msg)
  File "/usr/lib64/python3.4/http/client.py", line 898, in send
    self.connect()
  File "/usr/lib64/python3.4/http/client.py", line 1279, in connect
    super().connect()
  File "/usr/lib64/python3.4/http/client.py", line 871, in connect
    self.timeout, self.source_address)
  File "/usr/lib64/python3.4/socket.py", line 516, in create_connection
    raise err
  File "/usr/lib64/python3.4/socket.py", line 506, in create_connection
    sock.bind(source_address)
socket.gaierror: [Errno -9] Address family for hostname not supported

ipv6 is ok:

ping6 -n afraid.org
PING afraid.org(2604:8b80:2:8::2) 56 data bytes
64 bytes from 2604:8b80:2:8::2: icmp_seq=1 ttl=45 time=173 ms

Solution

  • You can only set the local address to the same address family as the remote. For you to be able to use an IPv6 local address, that requires the remote address to be available as IPv6 too.

    Unfortunately, that's not the case here; there is no IPv6 DNS entry for freedns.afraid.org:

    $ host -t AAAA freedns.afraid.org
    freedns.afraid.org has no AAAA record
    

    Note that freedns.afraid.org is not the same address as afraid.org; the latter does have an IPv6 address, which is why you are able to ping it:

    $ host -t AAAA afraid.org
    afraid.org has IPv6 address 2604:8b80:2:8::2