import socket
# the public network interface
HOST = socket.gethostbyname(socket.gethostname())
# create a raw socket and bind it to the public interface
s = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_IP)
s.bind((HOST, 0))
# Include IP headers
s.setsockopt(socket.IPPROTO_IP, socket.IP_HDRINCL, 1)
# receive all packages
s.ioctl(socket.SIO_RCVALL, socket.RCVALL_ON) //Error Occurs on this line
# receive a package
print(s.recvfrom(65565))
# disabled promiscuous mode
s.ioctl(socket.SIO_RCVALL, socket.RCVALL_OFF)
当我尝试运行此代码时,我得到:
Traceback (most recent call last):
File "D:/Users/Gen/PycharmProjects/hex2float/socketerror.py", line 14, in <module>
s.ioctl(socket.SIO_RCVALL, socket.RCVALL_ON)
OSError: [WinError 10045] The attempted operation is not supported for the type of object referenced
此代码取自Python文档,所以我很困惑为什么它不起作用。任何人都可以解释原因吗?
答案 0 :(得分:2)
也许你需要从你的句子socket.AF_INET
s = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_IP)
因为我使用SOCK_DGRAM
而没有AF_INET
来成功构建服务器。