Open losintikfos opened 6 years ago
In my quest to fetch the peer address - I am passing instance ofg zerorpc.Server
to a middleware class. And then within the server_before_exec
function trying something like this:
socket = self.server._events._socket.get_monitor_socket()
msg = socket.recv_multipart(0)
src_fd = msg.get(zmq.SRCFD)
src_sock = s.socket(fileno=src_fd)
print( "Peer address: %s" % src_sock.getpeername())
The problem is - the call to socket.recv_multipart(0)
blocks forever. Anyone have a clue how to achieve something like this?
You would have to read the monitor socket asynchronously. For this you need to use zeromq.green or come up with your own async io wrapper similar to gevent_zmq.py
http://api.zeromq.org/4-2:zmq-socket-monitor
But anyway, here is what the doc says about the ZMQ_EVENT_CONNECTED event:
The socket has successfully connected to a remote peer. The event value is the file descriptor (FD) of the underlying network socket. Warning: there is no guarantee that the FD is still valid by the time your code receives this event.
You should probably not call getpeername()
on a random integer value.
Conclusion: you cannot get the peer of a zeromq connection.
Is it possible to fetch remote peer address i.e possibly doing something similar to below: