Python 如何从websocket(客户端)打印流式传输信息?

Python 如何从websocket(客户端)打印流式传输信息?,python,websocket,client,Python,Websocket,Client,我想使用websocket打印流媒体信息。服务器间歇性地发送信息。我正在使用下面python代码中的而True:循环打印它 有更好的办法吗 from websocket import create_connection def connect_Bitfinex_trades(): ws = create_connection("wss://api.sample.com:3000/ws") print "Sent" while True: print "

我想使用websocket打印流媒体信息。服务器间歇性地发送信息。我正在使用下面python代码中的
而True:
循环打印它

有更好的办法吗

from websocket import create_connection


def connect_Bitfinex_trades():
    ws = create_connection("wss://api.sample.com:3000/ws")
    print "Sent"
    while True:
        print "Receiving..."
        result = ws.recv()
        print "Received '%s'" % result

我正在使用这里找到的websocket客户端

我个人认为这是从websocket获取/打印信息的更好解决方案。我在websocket客户端的开发人员网站上找到了这个示例

如果您注意到,此示例使用run_forever方法,该方法将保持websocket连接打开并接收消息,直到出现错误或连接关闭

import websocket
import thread
import time

def on_message(ws, message):
    print(message)

def on_error(ws, error):
    print(error)

def on_close(ws):
    print("### closed ###")

def on_open(ws):
    def run(*args):
        for i in range(3):
            time.sleep(1)
            ws.send("Hello %d" % i)
        time.sleep(1)
        ws.close()
        print("thread terminating...")
    thread.start_new_thread(run, ())


if __name__ == "__main__":
    websocket.enableTrace(True)
    ws = websocket.WebSocketApp("ws://echo.websocket.org/",
                              on_message = on_message,
                              on_error = on_error,
                              on_close = on_close)
    ws.on_open = on_open
    ws.run_forever()