import websocket
import base64
import time
import argparse
channel = "public-ch"
url = "ws://sockets.mbed.org/ws/" + channel + "/rw";
CHUNK=2*30
def buf_to_websocket(buf, ws=None):
print "image size: %d bytes" % len(buf)
st = time.time()
ws_send = 0
for i in range(0, len(buf), CHUNK):
chunk_len = min(len(buf) - i, CHUNK)
s = buf[i:i+chunk_len]
d = base64.b64encode(s)
print d
if ws:
ws.send(d)
ws_send += len(d)
d = "."
print d
if ws:
ws.send(d)
ms = (time.time() - st) * 1000
print "websocket: send: %d bytes %d ms" % (ws_send, ms)
if __name__ == '__main__':
ws = websocket.create_connection(url)
parser = argparse.ArgumentParser()
parser.add_argument('infiles', nargs='*', help=u"image file")
args = parser.parse_args()
for i, filename in enumerate(args.infiles):
with open(filename, "rb") as f:
data = f.read()
print("input: %s %d bytes" % (filename, len(data)))
buf_to_websocket(data, ws)