OSError:[WinError 10022]提供了无效的参数-Windows 10 Python
![OSError:[WinError 10022]提供了无效的参数-Windows 10 Python](/upfiles/imgfile/2310/1ER220592a20-2b32.jpg)
我目前正在学习来自Java的python,偶然发现我找不到答案的错误.我使用的是Windows 10上的最新python版本,尽管我假设我遵循的教程适用于Linux.希望您仍然可以为我提供帮助.这是我的课程代码:
def main():
connection = socket.socket(socket.AF_INET, socket.SOCK_RAW,
socket.IPPROTO_IP)
#mainloop
raw_data, addr = connection.recvfrom(65536)
dest_mac, src_mac, eth_proto, data = ethernet_frame(raw_data)
print('\nEthernet Frame:')
print('Destination: {}, Source: {}, Protocol: {}'.format(dest_mac,
src_mac, eth_proto))
#unpack ethernet frame
def ethernet_frame(data):
dest_mac, src_mac, proto = struct.unpack('! 6s 6s H', data[:14])
return get_mac_addr(dest_mac), get_mac_addr(src_mac),
socket.htons(proto), data[14:]
#format MAC adress
def get_mac_addr(bytes_addr):
bytes_str = map('{:02x}'.format, bytes_addr)
return ':'.join(bytes_str).upper()
main()
在执行时,我收到以下错误:
OSError: [WinError 10022] An invalid argument was supplied
在“ connection.recvfrom(65536)”行中.
无论如何,是否将要在Linux上获得虚拟机?我应该等待吗?或者还有针对Windows的好的解决方案?
解决方法:
套接字在绑定或发送数据之前没有地址.在使用connection.bind((YOUR_IP,PORT))调用connection.recvfrom(65536)之前绑定套接字.