Skip to content

Instantly share code, notes, and snippets.

@ksasao
Last active September 2, 2015 15:11
Show Gist options
  • Save ksasao/11018695fc7562c14cff to your computer and use it in GitHub Desktop.
Save ksasao/11018695fc7562c14cff to your computer and use it in GitHub Desktop.
UDPの特定のポートを時刻付きで録画再生。 License: WTFPL
# -*- coding: utf-8 -*-
import socket
import sys
import time
host = '127.0.0.1'
port = 21730
# open file
argvs = sys.argv
argc = len(argvs)
if (argc != 2):
print 'Usage: python %s filename' % argvs[0]
quit()
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
print 'UDP ' + host + ' ' + str(port)
print "sending datagram..."
f = open(argvs[1])
starttime = time.time()
line = f.readline()
while line:
pos = line.find(',')
if (pos >= 0):
nextsend = float(line[0:pos-1])
send_msg = line[pos+1:-1]
tick = nextsend - time.time() + starttime
if (tick > 0):
time.sleep(tick)
print unicode(line, 'utf-8').encode('shift-jis').strip()
sock.sendto(send_msg, (host, port))
line = f.readline()
# -*- coding: utf-8 -*-
import socket
import time
import datetime
host = '127.0.0.1'
port = 21730
# create filename
now = datetime.datetime.today()
name = 'UDP_' + now.strftime("%Y%m%d_%H%M%S") + '.csv'
fw = open(name, 'w')
# start record
starttime = time.time()
clientsock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
clientsock.bind((host, port))
print 'UDP ' + host + ' ' + str(port)
print "waiting datagram..."
while True:
recv_msg, addr = clientsock.recvfrom(1024)
currenttime = time.time() - starttime
output = str(currenttime) + ',' + recv_msg
print unicode(output, 'utf-8').encode('shift-jis')
fw.write(output + '\n')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment