0mq how to get the bound address

I am using zmq bindings for python and I want to bind clients to some port and then send this location to other clients for connection. Now this is the base binding code:

subscriber = context.socket(zmq.SUB) ... subscriber.bind("tcp://0.0.0.0:12345") 

I need to get the external address not 0.0.0.0. Say, on my computer ip 192.168.1.10, I need to get "tcp: //192.168.1.10: 12345" and send it to another client, because sending "tcp: //0.0.0.0: 12345" is useless. How can I get the external ip of the interface that zmq used to create the socket. There may be a NIC number on the PC, and if I just try to get an external ip using a regular socket, this might be invalid because I don’t know what zMq NIC uses.

+4
source share
1 answer

0.0.0.0 (INADDR_ANY) is an "any" address (a non-routable meta address). This is a way to specify "any IPv4 interface in general." This means that all of your interfaces are listening on port 12345.

To list all network interfaces, I would recommend using a library like this

If you are using linux, you can do this:

 import array import struct import socket import fcntl SIOCGIFCONF = 0x8912 #define SIOCGIFCONF BYTES = 4096 # Simply define the byte size # get_iface_list function definition # this function will return array of all 'up' interfaces def get_iface_list(): # create the socket object to get the interface list sck = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # prepare the struct variable names = array.array('B', '\0' * BYTES) # the trick is to get the list from ioctl bytelen = struct.unpack('iL', fcntl.ioctl(sck.fileno(), SIOCGIFCONF, struct.pack('iL', BYTES, names.buffer_info()[0])))[0] # convert it to string namestr = names.tostring() # return the interfaces as array return [namestr[i:i+32].split('\0', 1)[0] for i in range(0, bytelen, 32)] # now, use the function to get the 'up' interfaces array ifaces = get_iface_list() # well, what to do? print it out maybe... for iface in ifaces: print iface 
+1
source

Source: https://habr.com/ru/post/1400906/


All Articles