[zeromq-dev] Change of type after sending over

Hor Meng Yoong yoonghm at gmail.com
Mon Dec 26 07:27:40 CET 2011


Hi:

  I try to send something over using Python binding:

client.py

import struct
import time
import random
import zmq

ctx = zmq.Context()
s = ctx.socket(zmq.REQ)
s.connect('tcp://127.0.0.1:5555')
msg = b'ABCD'
print('Type of ', msg, 'is ', type(msg))
while True:
  print('send', msg, len(msg))
  s.send(msg)
  msg = s.recv()
  print('<', msg, len(msg))
  time.sleep(random.random() * 3)

*Type of  b'ABCD' is  <class 'bytes'>*

server.py
import struct
import time
import zmq
from zmq.eventloop import ioloop, zmqstream
loop = ioloop.IOLoop.instance()

ctx = zmq.Context()
s = ctx.socket(zmq.REP)
s.bind('tcp://127.0.0.1:5555')
stream = zmqstream.ZMQStream(s, loop)

def echo(msg):
  print('receive', msg, len(msg))
  print('Type of ', msg, 'is ', type(msg))
  (a,), msg = struct.unpack('B', msg[:1]), msg[1:]
  print('a = %c ' % a)
  if (a == 'A'):
    stream.send(b'a')
  elif (a == 'B'):
    stream.send(b'b')
  else:
    stream.send(b'z')

stream.on_recv(echo)
loop.start()

*Type of  [b'ABCD'] is  <class 'list'>*

As the received msg is in list, I could not use struct.unpack:
TypeError: 'list' does not support the buffer interface

Is this a bug or a normal behavior for zmq send()/receive() to change the
type?

Regards
Hor Meng
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.zeromq.org/pipermail/zeromq-dev/attachments/20111226/86fd8d2c/attachment.htm>


More information about the zeromq-dev mailing list