title: Azure Cognitive Services - Custom Speech with python and websockets
body: <p>I am using Microsoft Custom Speech Services within Python. Currently only with HTTP endpoints.
  Accordingly to the documentation, websockets are also supported.</p>
  
  <p>Does any one has an example on sending data through websockets?
  I got working so far opening websocket to service with my token. But when I start sending data, the connection closes with error 104.</p>
  
  <p>Details:
  - Python3
  - websocket-client
  - wav with RIFF header (works with HTTP)</p>
  
  <p>thx!</p>
  
  <p>Code sample:</p>
  
  <pre><code># pip install websocket-client
  def main_websocket_cris():
      path_root = os.path.abspath(os.path.dirname(__file__))
      filename = os.path.join(path_root, 'example_011.wav')
      chunk_size = 8192
  
      key = '&lt;mykey&gt;'
      url = 'wss://&lt;mydeployment&gt;.api.cris.ai/ws/cris/speech/recognize'
      token = auth_cris(key)
  
      header = ['Authorization: Bearer %s' % token]
      ws = websocket.create_connection(url, header=header)
      try:
          print('--- send ping')
          ws.ping()
          print('&gt; ping done')
  
          print('--- send pong')
          ws.pong(b'')
          print('&gt; pong done')
  
          print('--- status and headers')
          print('&gt; status:',  ws.getstatus())
          print('&gt; headers:', ws.getheaders())
          print('&gt; status done')
  
          headers = ['Path: audio',
                     'X-RequestId: %s' % str(uuid.uuid4()).replace('-', ''),
                     'X-Timestamp: %s' % datetime.datetime.now().isoformat(),
                     'Content-Type: audio/x-wav']
          headers = {
              'Path':         'audio',
              'X-RequestId':  str(uuid.uuid4()).replace('-', ''),
              'X-Timestamp':  str(datetime.datetime.now().isoformat()),
              'Content-Type': 'audio/x-wav'
          }
          print(headers)
          #ws.send(json.dumps(headers))
  
          print('--- send binary data')
          print('&gt; read file in chunks of %s bytes' % chunk_size)
          with open(filename, 'rb') as f:
              while True:
                  chunk = f.read(chunk_size)
                  if not chunk:
                      break
                  ws.send(json.dumps(headers))
                  ws.send_binary(chunk)
          print("&gt; Sent")
          print('--- now receive answer')
          print("&gt; Receiving...")
          result = ws.recv()
          print("&gt; Received '%s'" % result)
      finally:
          print('--- close')
          ws.close()
          print('&gt; closed')
  </code></pre>