错误10054:现有连接已关闭|使用ssl模块对https进行分块编码

问题描述

在这里,我试图将在ESP32芯片上将流音频复制到wit.ai的语音识别API中的操作(这就是为什么我不使用一些更高级别的模块的原因)。 我想我对HTTP分块编码synthax做错了,但我不知道是什么。

import socket
import ssl

hostname = 'api.wit.ai'
endpoint = "/speech"
context = ssl.create_default_context()

f = open("./main/test.raw","rb")
buffers = []
while True:
    buffer = f.read(50000)
    if not len(buffer):
        print("Reading over")
        break
    else:
        buffers.append(buffer)

with socket.create_connection((hostname,443)) as sock:
    with context.wrap_socket(sock,server_hostname=hostname) as ssock:
        ssock.sendall(bytes(endpoint + " HTTP/1.1\r\n","ascii"))
        ssock.sendall(bytes("Host: " + hostname + "\r\n","ascii"))
        ssock.sendall(bytes("Connection: keep-alive","ascii"))
        ssock.sendall(bytes("Authorization: Bearer "+ token + "\r\n","ascii"))
        ssock.sendall(bytes("Content-Type: audio/rawencoding=signed-integerbits=8rate=50000endian=little\r\n","ascii"))
        ssock.sendall(bytes("transfer-encoding: chunked\r\n","ascii"))
        ssock.sendall(bytes("\r\n","ascii"))

        for buffer in buffers:
            hexStr = hex(len(buffer))
            ssock.sendall(bytes(hexStr,'ascii'))
            ssock.sendall(bytes("\r\n",'ascii'))
            ssock.sendall(buffer)
            ssock.sendall(bytes("\r\n",'ascii'))

        ssock.sendall(bytes("0\r\n","ascii"))
        while (ssock.pending() > 0):
            print(ssock.read(1024))

使用上面的代码,出现此错误

  File "c:/Users/Antonin/Documents/Arduino/google home maison/main/test_ssl_sock.py",line 33,in <module>
    ssock.sendall(buffer)
  File "C:\Users\Antonin\AppData\Local\Programs\Python\python38-32\lib\ssl.py",line 1204,in sendall
    v = self.send(byte_view[count:])
  File "C:\Users\Antonin\AppData\Local\Programs\Python\python38-32\lib\ssl.py",line 1173,in send
    return self._sslobj.write(data)
ConnectionResetError: [WinError 10054]An existing connection was forcibly closed by the remote host```
(In my code the `token` variable is assigned,I just don't want to share it ^^ )

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)