如何使用websocket客户端发送二进制数据python使用WebSocketApp(不创建连接)
我选择WebSocketApp是因为它可以永远保持连接。不幸的是,WebSocketApp不提供ws.send_binary(),就像创建_连接一样。我想发送二进制数据消息并解码传入消息,请帮助我,以下是原始示例:如何使用websocket客户端发送二进制数据python使用WebSocketApp(不创建连接),python,websocket,Python,Websocket,我选择WebSocketApp是因为它可以永远保持连接。不幸的是,WebSocketApp不提供ws.send_binary(),就像创建_连接一样。我想发送二进制数据消息并解码传入消息,请帮助我,以下是原始示例: import websocket def on_message(ws, message): print(message) def on_error(ws, error): print(error) def on_close(ws): print('Websoc
import websocket
def on_message(ws, message):
print(message)
def on_error(ws, error):
print(error)
def on_close(ws):
print('Websocket: closed')
def on_open(ws):
print('Websocket: open')
ws = websocket.WebSocketApp('ws://echo.websocket.org/',on_message = on_message,on_error = on_error,on_close = on_close,on_open = on_open)
ws.run_forever()
您可以使用或(ws.send(buf,binary=True)
),两者都可以直接发送二进制数据。试试这个:
ws.send(data, websocket.ABNF.OPCODE_BINARY)
您可以使用base64对二进制数据进行编码,并将其作为文本传输。