test01.py 1.87 KB
import time
from websocket import create_connection


def connect_node(index, ques):
    t0 = time.time()
    ans = ""
    url = f"ws://192.168.3.191:8504/ws/{index}"
    ws = create_connection(url)
    ws.send(ques)
    while True:
        try:
            recv_text = ws.recv()
            print(index, recv_text)
            if "<rdfast stop>" in recv_text:
                print(
                    index,
                    "耗时为:",
                    round(time.time() - t0, 2),
                    "s,结果为:",
                    recv_text,
                )
                print(index, "break")
                break
            ans += recv_text
        except Exception as e:
            print(index, "except: ", str(e))
            recv_text = ws.recv()
            break
    print(index, ans)
    ws.close()

def connect_node_01(index, ques):
    t0 = time.time()
    ans = ""
    # url = f"ws://127.0.0.1:8000/ws/{index}"
    url = f"ws://127.0.0.1:23456/ws/"
    ws = create_connection(url)
    ws.send(ques)
    while True:
        try:
            recv_text = ws.recv()
            print(index, recv_text)
            if "<rdfast stop>" in recv_text:
                print(
                    index,
                    "耗时为:",
                    round(time.time() - t0, 2),
                    "s,结果为:",
                    recv_text,
                )
                print(index, "break")
                break
            ans += recv_text
        except Exception as e:
            print(index, "except: ", str(e))
            recv_text = ws.recv()
            break
    print(index, ans)
    ws.close()

def countdown(n):
    while n > 0:
        yield n
        n -= 1


if __name__ == "__main__":
    # connect_node(1,{"question":"你好"})
    # connect_node_01(1,{"question":"你好"})
    for num in countdown(5):
        print(num)
# end main