Skip to content

Instantly share code, notes, and snippets.

@gerkey
Created April 19, 2016 16:25
Show Gist options
  • Save gerkey/bf749775e6bc600368b97ce3d9f113e5 to your computer and use it in GitHub Desktop.
Save gerkey/bf749775e6bc600368b97ce3d9f113e5 to your computer and use it in GitHub Desktop.
Script to read pcap files and play them back as UDP packets (meant for Velodyne testing)
#!/usr/bin/env python
# Read a .pcap file full of UDP packets from a velodyne and play them back to
# localhost:2368, for consumption by the velodyne driver.
#
# TODO: error-checking and options (looping, etc.)
import dpkt
import sys
import socket
import time
UDP_IP = "localhost"
UDP_PORT = 2368
def parse(fname):
lasttime = -1
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
i = 0
with open(fname) as f:
pcap = dpkt.pcap.Reader(f)
for ts, buf in pcap:
eth = dpkt.ethernet.Ethernet(buf)
ip = eth.data
udp = ip.data
velodata = udp.data
if lasttime > 0 and (ts-lasttime) > 0:
time.sleep(ts-lasttime)
lasttime = ts
print('[%d] [%s] sending %d-byte message'%(i,`ts`,len(velodata)))
sock.sendto(velodata, (UDP_IP, UDP_PORT))
i += 1
if __name__ == '__main__':
if len(sys.argv) < 2:
print('ERROR: must supply pcap filename')
sys.exit(1)
while True:
parse(sys.argv[1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment