ZMQ Socket TypeError: unicode strings only Error: is there a fix?

2.6k views Asked by At

Trying to run the following Python code in command prompt: I'm using Python 2.

import zmq
context = zmq.Context()
socket = context.socket(zmq.SUB)
socket.connect('tcp://0.0.0.0.:6667')
socket.setsockopt_string(zmq.SUBSCRIBE, 'value')

and getting the following error when I execute:

socket.setsockopt_string(zmq.SUBSCRIBE, value)   File "C:\Program Files\Anaconda2\lib\site-packages\zmq\sugar\socket.py", line 192, in >set_string
raise TypeError("unicode strings only") TypeError: unicode strings only

can you pls advise on a solution ?

1

There are 1 answers

1
Fujiao Liu On BEST ANSWER

socket.setsockopt_string accepts unicode string for optval.

if you only run your code in python2, you should use

sock.setsockopt(zmq.SUBSCRIBE, b"value")

if you want to support both python2 and python3, you could use

try:
    sock.setsockopt(zmq.SUBSCRIBE, b'value')
except TypeError:
    sock.setsockopt_string(zmq.SUBSCRIBE, b'value')

Take a look at http://pyzmq.readthedocs.io/en/latest/unicode.html