Python sendto 似乎没有发送 [英] Python sendto doesn't seem to send

查看:75
本文介绍了Python sendto 似乎没有发送的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Python 新手,并尝试使用套接字将字节数组作为原始数据包发送.我的 IP 是 192.168.0.116,我发送这个的设备是 192.168.0.64.这个客户端设备是一个基于微控制器的单元,它正在执行我的代码来简单地嗅探以太网数据包并检查特定的模式.我正在使用 UDP 数据包,并通过在我的 PC 中使用Ostinato"发送原始 UDP 数据包来尝试客户端固件.我正在使用 Wireshark 来监控网络数据包流.客户端似乎可以很好地处理 Ostinato 发送的数据包.

I am new to Python and trying to send a byte array as a raw packet using a socket. My IP is 192.168.0.116 and the device I am sending this to is 192.168.0.64. This client device is a microcontroller based unit which is executing my code to simply sniff the Ethernet packets and check for a particular pattern. I am using UDP packets and tried the client side firmware by using 'Ostinato' in my PC to send a raw UDP packet. I am using Wireshark to monitor the network packet flow. The client seems to work fine with packets sent by Ostinato.

但是,当我尝试使用 Python 发送相同的数据包(使用如下原始数据包)时,它似乎没有吐出字节,因为我在 Wireshark 上看不到任何内容,客户端也无法获取任何数据.但是 sendto() 的返回值是正确的.因此,Python 似乎将字节数组提供给要发送的缓冲区(由操作系统?),但停在那里.

However when I try to send the same packet with Python (using raw packets as follows), it doesn't seem to spit out the bytes as I cant see anything on Wireshark nor the client get any data. But the return value from sendto() is correct. So it seems that Python feeds the byte array to a buffer 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()

为了清楚起见,我已经去掉了 try-except 块.

I have taken out the try-except blocks for clarity.

我注意到的另一件事是,当套接字关闭时,需要一些时间.如果我注释掉 sendto 语句,它会立即退出.因此,套接字关闭似乎正在尝试刷新发送缓冲区,但未能发送数据包.

Another thing I noted is that when the socket is closing, it takes a bit of time. If I comment out the sendto statement, it exits immediately. So it seems like the socket close is trying to flush the send buffers, which failed to send the packet.

有什么想法吗?

推荐答案

  1. Ilya 是对的,您应该使用

  1. Ilya is right, you should be opening a UDP socket with

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

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

您绑定到无效的端口 0.如果这不是入站端口,则不需要绑定它.这可以解释为什么您的 sendto 调用被阻塞.

You're binding to port 0, which is invalid. If this is not an inbound port, you don't need to bind it. This may explain why your sendto call is blocking.

send_data 数组应该只包含您的数据,而不是完整的以太网数据包".

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

send_data 数组必须小于您网络的 MTU 大小,否则它可能会被静默删除.它会有所不同,但低于 1300 应该可以工作,超过 1500 几乎肯定不会工作.

The send_data array must be under the MTU size for your network, otherwise it may be dropped silently. It varies but under 1300 should work, over 1500 almost certainly won't work.

这篇关于Python sendto 似乎没有发送的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆