套接字连接在3次迭代后中止-python

套接字连接在3次迭代后中止-python,python,sockets,websocket,Python,Sockets,Websocket,我正在开发一个应用程序,希望将音频流传输到网页。当我打开连接并发送消息时,服务器和客户端会收到消息(s.send('Hi'))。但当我开始循环时,我的脚本在3次迭代后停止,然后退出 我注意到服务器在第一次迭代后抛出一个从空值创建默认对象的错误 import websocket # https://github.com/liris/websocket-client HOST = 'ws://127.0.0.1:8080' s = websocket.create_connection(HOST

我正在开发一个应用程序,希望将音频流传输到网页。当我打开连接并发送消息时,服务器和客户端会收到消息
(s.send('Hi'))
。但当我开始循环时,我的脚本在3次迭代后停止,然后退出

我注意到服务器在第一次迭代后抛出一个
从空值创建默认对象的
错误

import websocket
# https://github.com/liris/websocket-client

HOST = 'ws://127.0.0.1:8080'

s = websocket.create_connection(HOST)

s.send('Hii')

i = 0
while 1:
    i += 1
    print(i)
    s.send(str(i))
    time.sleep(0.5)
这是完整的错误回溯

1
2
3
Traceback (most recent call last):
  File "E:\Redux\Win32\streamingdata.py", line 20, in <module>
    s.send(str(i))
  File "C:\Python33\lib\site-packages\websocket\__init__.py", line 656, in send
    return self.send_frame(frame)
  File "C:\Python33\lib\site-packages\websocket\__init__.py", line 680, in send_frame
    l = self._send(data)
  File "C:\Python33\lib\site-packages\websocket\__init__.py", line 900, in _send
    return self.sock.send(data)
ConnectionAbortedError: [WinError 10053] An established connection was aborted by the software in your host machine
1
2.
3.
回溯(最近一次呼叫最后一次):
文件“E:\Redux\Win32\streamingdata.py”,第20行,在
s、 发送(str(i))
文件“C:\Python33\lib\site packages\websocket\\uuuu init\uuuu.py”,第656行,在send中
返回自发送帧(帧)
文件“C:\Python33\lib\site packages\websocket\\uuuu init\uuuuu.py”,第680行,在send\u框架中
l=自发送(数据)
文件“C:\Python33\lib\site packages\websocket\\uuuuu init\uuuuuuu.py”,第900行,在发送中
返回self.sock.send(数据)
ConnectionAbortedError:[WinError 10053]主机中的软件中止了已建立的连接

哪里出了问题?使用纯套接字对远程侦听器执行脚本,服务器接收消息。使用websocket会失败。有人能帮我吗?

试着用
gevent.sleep(0.5)
替换你的
time.sleep(0.5)

由于
websocket
基于
gevent
,因此应防止使用阻塞操作

由于
time.sleep
正在阻止其中一个,您可能正在阻止通过
WebCocket
发送数据,因为它在您的睡眠期间没有机会进行通信

gevent.sleep
将给
gevent
greenlet一个生存的机会