识别ZMQ消息的来源? [英] Identifying the origin of ZMQ messages?

查看:20
本文介绍了识别ZMQ消息的来源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我通过 ZeroMQ (0MQ) 套接字上的 recv() 方法收到消息...

If I receive a message via the recv() method on a ZeroMQ (0MQ) socket...

data = s.recv()

...有什么方法可以让我获取底层套接字的 getpeername() 值?我的目标是以不依赖发件人提供准确信息的方式识别消息的来源.

...is there any way for me to get at the value of getpeername() for the underlying socket? My goal is to identify the origin of the message in a way that does not rely on the sender to provide accurate information.

我正在使用 ZMQ(通过 Python)来收集主机指标,从接收者的角度来看发送者的地址是一个有用的标识符.

I'm using ZMQ (via Python) to collect host metrics, and the address of the sender from the perspective of the receiver is a useful identifier.

或者这只是一个坏主意?

Or is this just a Bad Idea?

推荐答案

不,您无法从 ZeroMq 获取发件人的地址.你基本上有两个选择;将发件人地址信息添加到消息本身(如果允许修改现有消息结构,这不是一个坏选择)或将发件人地址添加为消息部分,即使用 ZeroMq 多部分消息.

No, you cannot get the address of the sender from ZeroMq. You basically have two choices; add the senders address information to the message itself (not a bad option if you are allowed to modify existing message structures) or add the sender address as a message part, i.e. use ZeroMq multi-part messages.

多部分消息仍将作为一个整体传送(所有部分或根本不传送),但您可以在接收端单独提取部分,因此您可以将发件人地址附加或预先添加到任何现有消息中,而无需实际上触摸它们(并且仍然将地址 + 消息作为原子操作传递).

A multi-part message will still be delivered as a whole (all parts or not at all), but you can extract the parts individually at the receiving end, thus you can append or prepend the sender address to any existing messages without actually touching them (and still deliver both address + message as an atomic operation).

我不确定这是如何在 pyzmq 绑定中实现的,但请查看 socket.pyx 源代码了解详情(基本上,在 send(..) 方法中使用 SNDMORE 标志).

I am not sure how this is implemented in the pyzmq binding, but check out the socket.pyx source for details (basically, use the SNDMORE flag in the send(..) method).

另外,看看 ZeroMq zmq_send() Api文档 (3.2.2).

Also, have a look at the ZeroMq zmq_send() Api docs (3.2.2).

在 C++ 中,它看起来像这样:

In C++, it would look something like this:

// Send a multi-part message consisting of sender IP plus another message
zmq_msg_send (&my_ip, my_socket, ZMQ_SNDMORE);
zmq_msg_send (&my_message, my_socket, 0);

这篇关于识别ZMQ消息的来源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆