使用 IPV6 的 UDP 套接字

UDP Socket using IPV6

大家好,我正在实现一个 bittorrent 客户端,但遇到了障碍。 这是我的代码,用于从仅处理 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.

出现以下错误

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

python socket documentation 表示 AF_INET6 地址必须作为 4 个值的元组传递。如果您传递一个 2 元组,它将被解释为 AF_INET (IPv4) 地址。