python创建websocket client

蒋嘉实
2023-12-01

最近需要测试同事写的websocket服务端的性能,为了便捷实用python 语言写了一段测试用例,创建300个线程,并且一直发送heart

from websocket import create_connection
import threading
import time
def fun(arg):

    try:
        ws = create_connection("ws://192.168.100.45:8088")
        rr = arg+110102;

        print rr

        login0 = "{\"action\":\"client.login\",\"data\":{\"devid\":\"FFFFFFFF-CCC8-2E74-FFFF-FFFF83DD28B6\",\"location\":\"%s\"}}"%(str(rr))
        heart = "{\"action\":\"heart.info\",\"data\":{\"devid\":\"FFFFFFFF-CCC8-2E74-FFFF-FFFF83DD28B6\",\"location\":\""+ str(rr) + "\",\"heart\":\"ping\"}}"

        ws.send(login0)
        print login0
        print heart
        print arg
        while 1:

            time.sleep(3)
            ws.send(heart)
            result = ws.recv()
            print result

        ws.close()

    except Exception as e:
        print (e)


def fun2():
    for i in range(1,1) :
        ws = create_connection("ws://192.168.100.45:8088")
        ws.send("Hello, World")
        result = ws.recv()
        print result
        ws.close()



if __name__ == '__main__':
    try:
        for i in range(1, 300):

            t = threading.Thread(target=fun, args=(i,))
            t.start()
            #time.sleep(1)
            #t.join()

    except Exception as e:
        print (e)


    while 1:
        time.sleep(1)
        pass

 

 类似资料: