我正在開發一個軟件定義的網絡研究項目,我需要的是製作一個簡單的UDP服務器,將數據標記放入UDP數據包的目標選項字段(IPv6)中。我期待sendmsg()recvmsg()命令,或者使用setsockopt()和getsockopt()。因此,Python 2.7沒有sendmsg()或recvmsg(),雖然我可以通過setsockopt()將正確的標籤加載到數據包中(我在Wireshark中看到它),但getsockopt()命令只返回0,即使標題在那裏。IPv6目標選項標題
#Python 2.7 client
#This code does put the dest opts header onto the packet correctly
#dst_header is a packed binary string (construction details irrelevant--
# it appears correctly formatted and parsed in Wireshark)
addr = ("::", 5000, 0, 0)
s = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM)
s.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_DSTOPTS, dst_header)
s.sendto('This is my message ', addr)
#Python 2.7 server
addr = ("::", 5000, 0, 0)
s = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM)
s.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_RECVDSTOPTS, 1)
s.bind(addr)
data, remote_address = s.recvfrom(MAX)
header_data = s.getsockopt(socket.IPPROTO_IPV6, socket.IPPROTO_DSTOPTS, 1024)
我也試過這個在Python 3.4,裏面確實有SENDMSG()和recvmsg(),但我只是得到一個錯誤信息 「OSERROR:[錯誤22]:無效的參數」,即使我」中號傳遞給它(顯然)正確類型:
s.sendmsg(["This is my message"], (socket.IPPROTO_IPV6, socket.IPV6_DSTOPTS, dst_header), 0, addr) #dst_header is same string as for 2.7 version
它看起來像SENDMSG()和recvmsg()是通過UNIX文件描述符,這是不是我想要做的使用率的99%。任何人有任何想法?我認爲這只是一個四五行沒有特別的計劃,但我很難過。