Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OSError: [WinError 10022] An invalid argument was supplied - Windows 10 Python

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

in the line with "connection.recvfrom(65536)".

Is this a windows specific error?

like image 752
Instinct Avatar asked Apr 10 '17 18:04

Instinct


1 Answers

The socket does not have an address until it is either bound or data is sent. Bind the socket before calling connection.recvfrom(65536) using connection.bind((YOUR_IP, PORT)).

like image 185
devautor Avatar answered Oct 18 '22 02:10

devautor