Establishing an IPv6 connection using sockets in python

I am trying to run this very basic socket example:

import socket host = 'ipv6hostnamegoeshere' port=9091 ourSocket = socket.socket(socket.AF_INET6, socket.SOCK_STREAM, 0) ourSocket.connect((host, port)) 

However, I get an error message:

  ourSocket.connect((host, port)) File "<string>", line 1, in connect socket.error: [Errno 22] Invalid argument 

Logical has_ipv6 returns true. Any help?

+6
python networking sockets ipv6
source share
1 answer

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

sockaddr is a tuple describing the socket address, the format of which depends on the returned family ((address, port) 2-tuple for AF_INET, a (address, port, stream information, area identifier). 4-tuple for AF_INET6) and should be passed to socket.connect () method.

For instance:

 >>> 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)) 
+11
source share

All Articles