[pyar] AYUDA: error WebSocket usando multi threading conectando con API

lukas lei lukaslei en hotmail.com
Lun Ene 15 10:26:03 ART 2018


Buenos dias.

Estoy trabajando en Python 3.6,  consumiendo un API mediante WebSocket (WS).

Tengo la siguiente situación:


Cuando abro una conexión WS y luego divido el procesamiento en 2 hilos y le envío msj al ws, el segundo hilo me da error al recibir las respuestas.

Abajo les dejo el ejemplo y el error. Estimo que debe ser algo en el reconocimiento entre el cliente y el srv que se rompe.


Alguien que me pueda ayudar?

desde ya muchas gracias, saludos.

Lucas


Seria algo así a modo de ejemplo:


from websocket import create_connection
import time, threading

def conecta(**datos):
    nro_ope = datos['nro_ope']
    ws = datos['ws']
    msg = datos['msg']
    print('ENTRO A ---------------' + str(nro_ope))
    print("Sending"+ str(nro_ope))
    ws.send(msg)
    print("Sent"+ str(nro_ope))
    contador = 0
    ws.settimeout(300)
    while contador < 10:
        contador += 1
        print(str(nro_ope) + "Receiving..." + str(contador))
        result =  ws.recv()
        print(str(nro_ope) + " => Received1" + str(result))

# //--- Cuerpo del scrip

ws = create_connection(url, cookie=cookie)
print("Sending")
mensaje1 = 'msj 1 al ws'
mensaje2 = 'msj 2 al ws'

hilo1 = threading.Thread(target=conecta, kwargs={'nro_ope': 1,'ws': ws, 'msg': mensaje1},  name='hilo1')
hilo1.setDaemon(False)
hilo1.start()

time.sleep(5)

hilo2  = threading.Thread(target=conecta,kwargs={'nro_ope': 2,'ws': ws, 'msg': mensaje2}, name='hilo2')
hilo2.setDaemon(False)
hilo2.start()

a = 1
while 1 == 1:
    a = a +1
    time.sleep(1)


ERROR:

Exception in thread hilo2:
Traceback (most recent call last):
  File "C:\Program Files\Python\Python36-32\lib\threading.py", line 916, in _bootstrap_inner
    self.run()
  File "C:\Program Files\Python\Python36-32\lib\threading.py", line 864, in run
    self._target(*self._args, **self._kwargs)
  File "C:/Users/user1/PycharmProjects/Desarrollo/pruebaWS_multiple.py", line 24, in conecta
    result =  ws.recv()
  File "C:\Program Files\Python\Python36-32\lib\site-packages\websocket\_core.py", line 293, in recv
    opcode, data = self.recv_data()
  File "C:\Program Files\Python\Python36-32\lib\site-packages\websocket\_core.py", line 310, in recv_data
    opcode, frame = self.recv_data_frame(control_frame)
  File "C:\Program Files\Python\Python36-32\lib\site-packages\websocket\_core.py", line 323, in recv_data_frame
    frame = self.recv_frame()
  File "C:\Program Files\Python\Python36-32\lib\site-packages\websocket\_core.py", line 357, in recv_frame
    return self.frame_buffer.recv_frame()
  File "C:\Program Files\Python\Python36-32\lib\site-packages\websocket\_abnf.py", line 336, in recv_frame
    self.recv_header()
  File "C:\Program Files\Python\Python36-32\lib\site-packages\websocket\_abnf.py", line 286, in recv_header
    header = self.recv_strict(2)
  File "C:\Program Files\Python\Python36-32\lib\site-packages\websocket\_abnf.py", line 371, in recv_strict
    bytes_ = self.recv(min(16384, shortage))
  File "C:\Program Files\Python\Python36-32\lib\site-packages\websocket\_core.py", line 427, in _recv
    return recv(self.sock, bufsize)
  File "C:\Program Files\Python\Python36-32\lib\site-packages\websocket\_socket.py", line 80, in recv
    bytes_ = sock.recv(bufsize)
  File "C:\Program Files\Python\Python36-32\lib\ssl.py", line 987, in recv
    return self.read(buflen)
  File "C:\Program Files\Python\Python36-32\lib\ssl.py", line 865, in read
    return self._sslobj.read(len, buffer)
  File "C:\Program Files\Python\Python36-32\lib\ssl.py", line 627, in read
    v = self._sslobj.read(len)
ssl.SSLError: [SSL: SSLV3_ALERT_BAD_RECORD_MAC] sslv3 alert bad record mac (_ssl.c:2178)

Exception in thread hilo1:
Traceback (most recent call last):
  File "C:\Program Files\Python\Python36-32\lib\threading.py", line 916, in _bootstrap_inner
    self.run()
  File "C:\Program Files\Python\Python36-32\lib\threading.py", line 864, in run
    self._target(*self._args, **self._kwargs)
  File "C:/Users/user1/PycharmProjects/Desarrollo/pruebaWS_multiple.py", line 24, in conecta
    result =  ws.recv()
  File "C:\Program Files\Python\Python36-32\lib\site-packages\websocket\_core.py", line 293, in recv
    opcode, data = self.recv_data()
  File "C:\Program Files\Python\Python36-32\lib\site-packages\websocket\_core.py", line 310, in recv_data
    opcode, frame = self.recv_data_frame(control_frame)
  File "C:\Program Files\Python\Python36-32\lib\site-packages\websocket\_core.py", line 323, in recv_data_frame
    frame = self.recv_frame()
  File "C:\Program Files\Python\Python36-32\lib\site-packages\websocket\_core.py", line 357, in recv_frame
    return self.frame_buffer.recv_frame()
  File "C:\Program Files\Python\Python36-32\lib\site-packages\websocket\_abnf.py", line 336, in recv_frame
    self.recv_header()
  File "C:\Program Files\Python\Python36-32\lib\site-packages\websocket\_abnf.py", line 286, in recv_header
    header = self.recv_strict(2)
  File "C:\Program Files\Python\Python36-32\lib\site-packages\websocket\_abnf.py", line 371, in recv_strict
    bytes_ = self.recv(min(16384, shortage))
  File "C:\Program Files\Python\Python36-32\lib\site-packages\websocket\_core.py", line 427, in _recv
    return recv(self.sock, bufsize)
  File "C:\Program Files\Python\Python36-32\lib\site-packages\websocket\_socket.py", line 80, in recv
    bytes_ = sock.recv(bufsize)
  File "C:\Program Files\Python\Python36-32\lib\ssl.py", line 987, in recv
    return self.read(buflen)
  File "C:\Program Files\Python\Python36-32\lib\ssl.py", line 865, in read
    return self._sslobj.read(len, buffer)
  File "C:\Program Files\Python\Python36-32\lib\ssl.py", line 627, in read
    v = self._sslobj.read(len)
BlockingIOError: [WinError 10035] A non-blocking socket operation could not be completed immediately



------------ próxima parte ------------
Se ha borrado un adjunto en formato HTML...
URL: <http://listas.python.org.ar/pipermail/pyar/attachments/20180115/2eccac1d/attachment-0001.html>


Más información sobre la lista de distribución pyar