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

26,658

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)).

Share:
26,658
Instinct
Author by

Instinct

Updated on November 17, 2021

Comments

  • Instinct
    Instinct about 1 year

    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?

  • fuzzything44
    fuzzything44 over 5 years
    Note that at that point you don't need to do recvfrom as recv will work just as well since you know the IP and port.
  • Yigit Alparslan
    Yigit Alparslan almost 3 years
    Is there a way to find the port number dynamically from client-side?