Home > Software engineering >  UDP Socket using IPV6
UDP Socket using IPV6

Time:10-15

Hello everyone, I am implementing a bittorrent client and i have hit a roadblock. Here is my code to get the peer list from a particular tracker which only deals in ipv6.

tracker = "udp://tracker.birkenwald.de:6969/announce"
parse = urlparse(tracker)
ip, port = (socket.getaddrinfo(parse.hostname, parse.port, 0, 0, socket.SOL_TCP))[0][4][0], parse.port
#ip, port = 2001:1b10:1000:8101:0:242:ac11:2,6969
sock = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM) 
sock.sendto(tracker_connection.bytestringForConnecting(), (ip, port))

#bytestringForConnecting() is a function for getting the byte version as written in the protocol.

Getting the following error

Traceback (most recent call last):
  File "main.py", line 17, in <module>
    t.get_peer_list()
  File "/home/aditya/Desktop/CN/CN PROJECT/tracker.py", line 21, in get_peer_list
    self.udp_request(url)
  File "/home/aditya/Desktop/CN/CN PROJECT/tracker.py", line 64, in udp_request
    sock.sendto(tracker_connection.bytestringForConnecting(), (ip, port))
socket.gaierror: [Errno -9] Address family for hostname not supported

CodePudding user response:

The python socket documentation says that AF_INET6 addresses must be passed as a tuple of 4 values. If you pass a 2-tuple it'll be interpreted as AF_INET (IPv4) address.

  • Related