使用发送方法pyzmq错误不支持检索操作

Retrieving operation not supported on using send method pyzmq error

我有以下代码将视频帧从服务器发送到客户端。我正在检索 server.py.

上的错误

Server.py

import base64
import cv2
import zmq
import time
import numpy as np
context = zmq.Context()
footage_socket = context.socket(zmq.SUB)
footage_socket.bind('tcp://0.0.0.0:5555')
footage_socket.setsockopt_string(zmq.SUBSCRIBE, np.unicode(''))

videoFile = 'SAMPLE.mp4'
camera = cv2.VideoCapture(videoFile)  # init the camera
length=int(camera.get(cv2.CAP_PROP_FRAME_COUNT))
while True:        
    grabbed, frame = camera.read()
    try:
       frame = cv2.resize(frame, (224, 224))
    except cv2.error:
        break
   
    encoded, buffer = cv2.imencode('.jpg', frame)
    jpg_as_text = base64.b64encode(buffer)
    time.sleep(3)
    footage_socket.send(jpg_as_text)
footage_socket.close()

Client.py

  import cv2,zmq,base64
  import numpy as np
  context = zmq.Context()
  footage_socket = context.socket(zmq.PUB)
  footage_socket.connect('tcp://10.96.0.1:5555')
  while True:
      frame = footage_socket.recv_string()
      source = cv2.imdecode( np.fromstring( base64.b64decode( frame ), dtype = np.uint8),1 )

我正在检索以下错误

  footage_socket.send(jpg_as_text)
  File "/usr/local/lib/python3.5/dist-packages/zmq/sugar/socket.py", line 391, in send
  return super(Socket, self).send(data, flags=flags, copy=copy, track=track)
  File "zmq/backend/cython/socket.pyx", line 727, in zmq.backend.cython.socket.Socket.send
  File "zmq/backend/cython/socket.pyx", line 774, in zmq.backend.cython.socket.Socket.send
  File "zmq/backend/cython/socket.pyx", line 249, in zmq.backend.cython.socket._send_copy
  File "zmq/backend/cython/socket.pyx", line 244, in zmq.backend.cython.socket._send_copy
  File "zmq/backend/cython/checkrc.pxd", line 25, in zmq.backend.cython.checkrc._check_rc
  zmq.error.ZMQError: Operation not supported

非常感谢您的帮助。

您的代码尝试在 Socket-class 实例上调用 .recv_string() 方法,该实例创建的类型为 PUB.

那永远行不通。 PUB Scalable Formal Communication Archetype 属于 some-PUBlish + many-can-SUBscribe 以接收与他们匹配的所有 PUBlished 数据片段活动SUB描述详情。

ZeroMQ API 规范有详细记录和发布,对此有明确规定。 PUB 可以 .send() 但永远不会 .recv()

Server.py 中的 SUB-socket 实例也会发生同样的情况,其中您的代码 (as-is) 命令调用 .send(),这对于 SUB-socket 永远不会发生。

zmq.error.ZMQError: Operation not supported 是此类 API-colliding 尝试的唯一结果。