OSError: [WinError 10022] 提供了无效参数 - Windows 10 Python [英] OSError: [WinError 10022] An invalid argument was supplied - Windows 10 Python
问题描述
我目前正在学习 python,来自 java,但偶然发现了一个我找不到答案的错误.我在 Windows 10 上使用最新的 python 版本,但我认为我遵循的教程是针对 Linux 的...希望你仍然可以帮助我.这是我的类代码:
I am currently learning python, coming from java, and stumbled into an error I can't find the answer to. I am using the latest python version on Windows 10, though I assume the tutorial I followed was meant for Linux... Hope you can still help me out. This is my classcode:
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()
执行时我收到以下错误:
On execution I receive following Error:
OSError: [WinError 10022] An invalid argument was supplied
在connection.recvfrom(65536)"行中.
in the line with "connection.recvfrom(65536)".
这是特定于 Windows 的错误吗?
Is this a windows specific error?
推荐答案
套接字在绑定或发送数据之前没有地址.在使用 connection.bind((YOUR_IP, PORT))
调用 connection.recvfrom(65536)
之前绑定套接字.
The socket doesn't have an address until its either binded or data is sent. Bind the socket before calling connection.recvfrom(65536)
using connection.bind((YOUR_IP, PORT))
.
这篇关于OSError: [WinError 10022] 提供了无效参数 - Windows 10 Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!