Python sendto doesn't seem to send

I am new to Python and trying to send an array of bytes as a raw packet using a socket. My IP address is 192.168.0.116 , and the device I'm sending is 192.168.0.64 . This client device is a microcontroller-based unit that executes my code to simply sniff out Ethernet packets and verify a specific pattern. I use UDP packets and tried the client-side firmware using "Ostinato" on my PC to send the raw UDP packet. I use Wireshark to monitor the flow of network packets. The client seems to be working fine with packages sent by Austinato.

However, when I try to send the same package with Python (using raw packages in the following way), it does not seem to spit out bytes since I cannot see anything on Wireshark and the client is not receiving any data. But the return value from sendto() correct. It looks like Python is passing an array of bytes to the buffer that needs to be sent (by OS?), But stops there.

 import socket CLNT_UDP_IP = '192.168.0.64' CLNT_UDP_PORT = 5005 svr_sock = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_RAW) send_data = [0x00, 0x11...] send_data_arr = bytearray (send_data) svr_sock.bind (('192.168.0.116',0)) bytes_send = svr_sock.sendto (send_data_arr, (CLNT_UDP_IP, CLNT_UDP_PORT)) svr_sock.close() 

I realized that for the exception, I chose try-except blocks.

Another thing I noticed is that when the socket closes, it takes a little time. If I comment on the sendto statement, it will exit immediately. Thus, it looks like socket close is trying to flush send buffers that could not send the packet.

Any ideas?

+5
source share
1 answer
  • Ilya is right, you must open a UDP socket with

    socket.socket (socket.AF_INET, socket.SOCK_DGRAM)

  • You are bound to port 0, which is not true. If this is not an inbound port, you do not need to bind it. This may explain why your call to sendto is blocked.

  • The send_data array should only contain your data, and not the "full ethernet packet."

  • The send_data array must be under the MTU size for your network, otherwise it may be silently disabled. It varies, but it should work until 1300, more than 1500 will almost certainly not work.

0
source

All Articles