【问题标题】:ZMQ Socket TypeError: unicode strings only Error: is there a fix?ZMQ Socket TypeError: unicode strings only 错误: 有修复吗?
【发布时间】:2017-05-15 10:52:55
【问题描述】:

尝试在命令提示符下运行以下 Python 代码: 我正在使用 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')

执行时出现以下错误:

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

您能建议解决方案吗?

【问题讨论】:

标签: python sockets typeerror pyzmq


【解决方案1】:

socket.setsockopt_string 接受 unicode 字符串作为 optval。

如果你只在python2中运行你的代码,你应该使用

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

如果你想同时支持python2和python3,你可以使用

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

看看http://pyzmq.readthedocs.io/en/latest/unicode.html

【讨论】:

  • 不幸的是,这对我不起作用 - 仍然是“仅 unicode 字符串”
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-16
  • 2020-11-21
  • 2020-02-03
  • 2019-09-14
  • 1970-01-01
相关资源
最近更新 更多