无法打开到IBM Watson speech to tex的websocket连接

2024-06-26 04:00:02 发布

您现在位置:Python中文网/ 问答频道 /正文

下面的代码只是返回输出:“未能打开WebSocket”请帮助我更正它。我怀疑网址可能有问题,但仍然不确定。在

from ws4py.client.threadedclient import WebSocketClient
import base64, time

class SpeechToTextClient(WebSocketClient):
    def __init__(self):
    ws_url = "wss://stream.watsonplatform.net/speech-to-text/api/v1/recognize"

    username = "your username"
    password = "your password"
    auth_string = "%s:%s" % (username, password)
    base64string = base64.b64encode(auth_string.encode())

    try:
        WebSocketClient.__init__(self, ws_url,
            headers=[("Authorization", "Basic %s" % base64string)])
        self.connect()
    except: print("Failed to open WebSocket.")

    def opened(self):
        self.send('{"action": "start", "content-type": "audio/l16;rate=16000"}')

def received_message(self, message):
    print(message)

stt_client = SpeechToTextClient()
time.sleep(3)
stt_client.close()

错误信息如下:

以下是完整的错误消息:

回溯(最近一次呼叫): 文件“C:\Users\Vetle\Desktop\Python projects\问题.py“,第27行,英寸 stt公司_客户端.close() 文件“C:\Users\Vetle\AppData\Local\Programs\Python\Python35\lib\site packages\ws4py\client_uinit_uu.py”,第205行,关闭 你自己写(self.stream.close.关闭(代码=代码,原因=原因)。单个(掩码=真) 文件“C:\Users\Vetle\AppData\Local\Programs\Python35\lib\site packages\ws4py\websocket.py“,第283行,in-write 引发RuntimeError(“无法在已终止的websocket上发送”) 运行时错误:无法在已终止的websocket上发送


Tags: 文件代码pyselfclientmessageclosedef