Last active
September 2, 2015 15:11
-
-
Save ksasao/11018695fc7562c14cff to your computer and use it in GitHub Desktop.
UDPの特定のポートを時刻付きで録画再生。 License: WTFPL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- 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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- 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