boortmans
boortmans

Reputation: 1170

Socket connection aborted after 3 iterations - python

I'm developing an application where I want to stream an audio stream to a web page. When I open a connection and send a message, the server and client receive the message (s.send('Hi')). But when I start a loop, my script stops after exact 3 iterations and then quits.

I noticed that the server throws an creating default object from empty value error after the first iteration

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)

This is the full error traceback

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

Where does it go wrong? Using pure sockets to a remote listener, the script executes and the server receives the messages. Using websocket it fails. Someone who can help me out?

Upvotes: 1

Views: 589

Answers (1)

Jan Vlcinsky
Jan Vlcinsky

Reputation: 44112

Try replacing your time.sleep(0.5) by gevent.sleep(0.5)

As websocket is based on gevent, you shall prevent using blocking operations.

As the time.sleep is blocking one, you are likely blocking sending the data by webscocket, which has no chance to communicate during your sleep.

gevent.sleep shall give gevent greenlet a chance to live.

Upvotes: 1

Related Questions