1. Bernhard Biskup
  2. araldo

Source

araldo / scripts / websocket_client.py

#!/usr/bin/env python
# adapted from
# https://github.com/liris/websocket-client/wiki/WebSocketWithGevent
# uses https://github.com/liris/websocket-client
import sys
from gevent import monkey
monkey.patch_all()
from websocket import create_connection, enableTrace
import time
import random

if __name__ == '__main__':
    msgLen = int(sys.argv[1])
    enableTrace(True)
    ws = create_connection("ws://localhost:8890/websocket")
    a = ord("a")
    message = "".join(
        [chr(int(a + random.random() * 26)) for x in range(msgLen)])
    print "Sending %d bytes of data..." % msgLen
    t1 = time.time()
    ws.send(message)
    print "Sent %d bytes of data..." % msgLen
    print "\tsend duration: %.f ms" % ((time.time() - t1) * 1000)
    print "Receiving..."
    t1 = time.time()
    result = ws.recv()
    print "\treceive duration: %.f ms" % ((time.time() - t1) * 1000)
    print "Received '%s'" % len(result)
    ws.close()