python udp 大文件_使用python接收高速率的UDP数据包

I'm working with python in order to receive a stream of UDP packets from an FPGA, trying to lose as few packets as possible.

The packet rate goes from around 5kHz up to some MHz and we want to take data in a specific time window (acq_time in the code).

We have this code now:

BUFSIZE=4096

dataSock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

dataSock.settimeout(0.1)

dataSock.bind((self.HOST_IP, self.HOST_PORT))

time0=time.time()

data_list = []

while time.time()-time0

fast_acquisition(data_list)

def fast_acquisition(data_list_tmp):

data, addr = dataSock.recvfrom(self.BUFSIZE)

data_list_tmp.append(data)

return len(data)

And after the acquisition we save our data_list on disk.

This code is meant to be as simple and fast as possible, but it's still too slow and we lose too many packets even at 5kHz, and we think that this happens because while we read, and store in the list one packet and check the time, the next one (ore ones) arrives and is lost.

Is there any way to keep the socket open? Can we open multiple sockets "in series" with parallel processing, so that when we are saving the file from the first the second can receive another packet?

We can even think to use another language only to receive and store the packets on disk.

解决方案

You could use tcpdump (which is implemented in C) to capture the UDP traffic, since it's faster than python:

#!/bin/bash

iface=$1 # interface, 1st arg

port=$2 # port, 2nd arg

tcpdump -i $iface -G acq_time_in_seconds -n udp port $port -w traffic.pcap

And then you can use e.g. scapy to process that traffic

#!/usr/bin/env python

from scapy.all import *

scapy_cap = rdpcap('traffic.pcap')

for packet in scapy_cap:

# process packets...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值