当前位置: 代码迷 >> python >> 带有自定义标头的Stomp.py发送失败
  详细解决方案

带有自定义标头的Stomp.py发送失败

热度:53   发布时间:2023-06-13 16:57:30.0

我目前正在将消息从自定义系统通过python Stomp.py库推送到ActiveMQ实例。 当我提供带有自定义标头的字典作为send命令中的“标头”参数时,此操作将失败。

destination = self.subscription_id.queue_name
# Connect to the server
conn.connect(username=$username,
             password=$password,
             headers={})
# Send the actual message out
conn.send(destination, self.body, headers=self.header)
conn.disconnect()

由于某种原因,标头无法为我提供此错误:

ValueError: dictionary update sequence element #0 has length 1; 2 is required

堆栈跟踪的最后一部分:

File "/custom_addons/activemq_message.py", line 124, in send_to_queue
conn.send(destination, self.body, headers=self.header)

File "/usr/local/lib/python2.7/dist-packages/stomp/protocol.py", line 151, in send
headers = utils.merge_headers([headers, keyword_headers])

File "/usr/local/lib/python2.7/dist-packages/stomp/utils.py", line 166, in merge_headers
headers.update(header_map)

不管我在字典中实际提供什么内容,还是只发送空的内容,这都是不管的。

这也与我在连接级别或发送(或两者)提供标头无关。

似乎在某些时候将标头转换为字符串,但是我无法弄清楚这是否是故意的。 我也找不到解决方法。

任何线索将不胜感激。

找到了原因,在代码的其他部分中,标头存储在字符串字段中。 随后,它尝试发送unicode代替字典。

由于无法编辑源代码,因此使用“ ast”模块的方法“ literal_eval”将unicode转换为字典,并且可以正常工作。