The Python Oracle

Establishing an IPv6 connection using sockets in python

Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn

--

Track title: CC H Dvoks String Quartet No 12 Ame

--

Chapters
00:00 Question
00:33 Accepted answer (Score 18)
01:21 Thank you

--

Full question
https://stackoverflow.com/questions/5358...

Accepted answer links:
[socket.connect docs]: http://docs.python.org/library/socket.ht...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #sockets #networking #ipv6

#avk47



ACCEPTED ANSWER

Score 20


As the socket.connect docs says, AF_INET6 expects a 4-tuple:

sockaddr is a tuple describing a socket address, whose format depends on the returned family (a (address, port) 2-tuple for AF_INET, a (address, port, flow info, scope id) 4-tuple for AF_INET6), and is meant to be passed to the socket.connect() method.

For example:

>>> socket.getaddrinfo("www.python.org", 80, 0, 0, socket.SOL_TCP)
[(2, 1, 6, '', ('82.94.164.162', 80)),
 (10, 1, 6, '', ('2001:888:2000:d::a2', 80, 0, 0))]

>>> ourSocket = socket.socket(socket.AF_INET6, socket.SOCK_STREAM, 0)
>>> ourSocket.connect(('2001:888:2000:d::a2', 80, 0, 0))