def test_frame_more(self): """test Frame.more attribute""" frame = zmq.Frame(b"hello") self.assertFalse(frame.more) sa,sb = self.create_bound_pair(zmq.PAIR, zmq.PAIR) sa.send_multipart([b'hi', b'there']) frame = self.recv(sb, copy=False) self.assertTrue(frame.more) if zmq.zmq_version_info()[0] >= 3 and not PYPY: self.assertTrue(frame.get(zmq.MORE)) frame = self.recv(sb, copy=False) self.assertFalse(frame.more) if zmq.zmq_version_info()[0] >= 3 and not PYPY: self.assertFalse(frame.get(zmq.MORE))
def test_proxy(self): if zmq.zmq_version_info() < (3,2): raise SkipTest("Proxies only in libzmq >= 3") dev = devices.ThreadProxy(zmq.PULL, zmq.PUSH, zmq.PUSH) binder = self.context.socket(zmq.REQ) iface = 'tcp://127.0.0.1' port = binder.bind_to_random_port(iface) port2 = binder.bind_to_random_port(iface) port3 = binder.bind_to_random_port(iface) binder.close() time.sleep(0.1) dev.bind_in("%s:%i" % (iface, port)) dev.bind_out("%s:%i" % (iface, port2)) dev.bind_mon("%s:%i" % (iface, port3)) dev.start() time.sleep(0.25) msg = b'hello' push = self.context.socket(zmq.PUSH) push.connect("%s:%i" % (iface, port)) pull = self.context.socket(zmq.PULL) pull.connect("%s:%i" % (iface, port2)) mon = self.context.socket(zmq.PULL) mon.connect("%s:%i" % (iface, port3)) push.send(msg) self.sockets.extend([push, pull, mon]) self.assertEqual(msg, self.recv(pull)) self.assertEqual(msg, self.recv(mon))
def get_monitor_socket(self, events=None, addr=None): """Return a connected PAIR socket ready to receive the event notifications. .. versionadded:: libzmq-4.0 .. versionadded:: 14.0 Parameters ---------- events : bitfield (int) [default: ZMQ_EVENTS_ALL] The bitmask defining which events are wanted. addr : string [default: None] The optional endpoint for the monitoring sockets. Returns ------- socket : (PAIR) The socket is already connected and ready to receive messages. """ # safe-guard, method only available on libzmq >= 4 if zmq.zmq_version_info() < (4,): raise NotImplementedError("get_monitor_socket requires libzmq >= 4, have %s" % zmq.zmq_version()) if addr is None: # create endpoint name from internal fd addr = "inproc://monitor.s-%d" % self.FD if events is None: # use all events events = zmq.EVENT_ALL # attach monitoring socket self.monitor(addr, events) # create new PAIR socket and connect it ret = self.context.socket(zmq.PAIR) ret.connect(addr) return ret
def test_proxy_steerable(self): if zmq.zmq_version_info() < (4, 1): raise SkipTest("Steerable Proxies only in libzmq >= 4.1") dev = devices.ThreadProxySteerable( zmq.PULL, zmq.PUSH, zmq.PUSH, zmq.PAIR ) iface = 'tcp://127.0.0.1' port = dev.bind_in_to_random_port(iface) port2 = dev.bind_out_to_random_port(iface) port3 = dev.bind_mon_to_random_port(iface) port4 = dev.bind_ctrl_to_random_port(iface) dev.start() time.sleep(0.25) msg = b'hello' push = self.context.socket(zmq.PUSH) push.connect("%s:%i" % (iface, port)) pull = self.context.socket(zmq.PULL) pull.connect("%s:%i" % (iface, port2)) mon = self.context.socket(zmq.PULL) mon.connect("%s:%i" % (iface, port3)) ctrl = self.context.socket(zmq.PAIR) ctrl.connect("%s:%i" % (iface, port4)) push.send(msg) self.sockets.extend([push, pull, mon, ctrl]) self.assertEqual(msg, self.recv(pull)) self.assertEqual(msg, self.recv(mon)) ctrl.send(b'TERMINATE') dev.join()
def set_hwm(self, value): """set the High Water Mark On libzmq ≥ 3, this sets both SNDHWM and RCVHWM .. warning:: New values only take effect for subsequent socket bind/connects. """ major = zmq.zmq_version_info()[0] if major >= 3: raised = None try: self.sndhwm = value except Exception as e: raised = e try: self.rcvhwm = value except Exception as e: raised = e if raised: raise raised else: return self.setsockopt(zmq.HWM, value)
def test_monitor(self): """Test monitoring interface for sockets.""" s_rep = self.context.socket(zmq.REP) s_req = self.context.socket(zmq.REQ) self.sockets.extend([s_rep, s_req]) s_req.bind("tcp://127.0.0.1:6666") # try monitoring the REP socket s_rep.monitor("inproc://monitor.rep", zmq.EVENT_ALL) # create listening socket for monitor s_event = self.context.socket(zmq.PAIR) self.sockets.append(s_event) s_event.connect("inproc://monitor.rep") s_event.linger = 0 # test receive event for connect event s_rep.connect("tcp://127.0.0.1:6666") if zmq.zmq_version_info() < (3,3): self.assertRaises(NotImplementedError, get_monitor_message, s_event) return m = get_monitor_message(s_event) self.assertEqual(m['event'], zmq.EVENT_CONNECT_DELAYED) self.assertEqual(m['endpoint'], b"tcp://127.0.0.1:6666") # test receive event for connected event m = get_monitor_message(s_event) self.assertEqual(m['event'], zmq.EVENT_CONNECTED)
def _zmq_has_curve(): """ Return whether the current ZMQ has support for auth and CurveZMQ security. :rtype: bool Version notes: `zmq.curve_keypair()` is new in version 14.0, new in version libzmq-4.0. Requires libzmq (>= 4.0) to have been linked with libsodium. `zmq.auth` module is new in version 14.1 `zmq.has()` is new in version 14.1, new in version libzmq-4.1. """ zmq_version = zmq.zmq_version_info() pyzmq_version = zmq.pyzmq_version_info() if pyzmq_version >= (14, 1, 0) and zmq_version >= (4, 1): return zmq.has('curve') if pyzmq_version < (14, 1, 0): return False if zmq_version < (4, 0): # security is new in libzmq 4.0 return False try: zmq.curve_keypair() except zmq.error.ZMQError: # security requires libzmq to be linked against libsodium return False return True
def enable_monitor(self, events=None): # The standard approach of binding and then connecting does not # work in this specific case. The event loop does not properly # detect messages on the inproc transport which means that event # messages get missed. # pyzmq's 'get_monitor_socket' method can't be used because this # performs the actions in the wrong order for use with an event # loop. # For more information on this issue see: # http://lists.zeromq.org/pipermail/zeromq-dev/2015-July/029181.html if (zmq.zmq_version_info() < (4,) or zmq.pyzmq_version_info() < (14, 4,)): raise NotImplementedError( "Socket monitor requires libzmq >= 4 and pyzmq >= 14.4, " "have libzmq:{}, pyzmq:{}".format( zmq.zmq_version(), zmq.pyzmq_version())) if self._monitor is None: addr = "inproc://monitor.s-{}".format(self._zmq_sock.FD) events = events or zmq.EVENT_ALL _, self._monitor = yield from create_zmq_connection( lambda: _ZmqEventProtocol(self._loop, self._protocol), zmq.PAIR, connect=addr, loop=self._loop) # bind must come after connect self._zmq_sock.monitor(addr, events) yield from self._monitor.wait_ready
def setUp(self): if zmq.zmq_version_info() < (4, 0): raise SkipTest("security is new in libzmq 4.0") try: zmq.curve_keypair() except zmq.ZMQError: raise SkipTest("security requires libzmq to be linked against libsodium") super(TestSecurity, self).setUp()
def setUp(self): if zmq.zmq_version_info() < (4,0): raise SkipTest("security is new in libzmq 4.0") try: zmq.curve_keypair() except zmq.ZMQError: raise SkipTest("security requires libzmq to be built with CURVE support") super(TestSecurity, self).setUp()
def test_ctx_opts(self): if zmq.zmq_version_info() < (3,): raise SkipTest("context options require libzmq 3") ctx = self.Context() ctx.set(zmq.MAX_SOCKETS, 2) self.assertEqual(ctx.get(zmq.MAX_SOCKETS), 2) ctx.max_sockets = 100 self.assertEqual(ctx.max_sockets, 100) self.assertEqual(ctx.get(zmq.MAX_SOCKETS), 100)
def setUp(self): if zmq.zmq_version_info() < (4, 0): raise SkipTest("security is new in libzmq 4.0") try: zmq.curve_keypair() except zmq.ZMQError: raise SkipTest( "security requires libzmq to be linked against libsodium") super(TestSecurity, self).setUp()
def test_ctx_opts(self): if zmq.zmq_version_info() < (3, ): raise SkipTest("context options require libzmq 3") ctx = self.Context() ctx.set(zmq.MAX_SOCKETS, 2) self.assertEqual(ctx.get(zmq.MAX_SOCKETS), 2) ctx.max_sockets = 100 self.assertEqual(ctx.max_sockets, 100) self.assertEqual(ctx.get(zmq.MAX_SOCKETS), 100)
def test_int_sockopts(self): "test integer sockopts" v = zmq.zmq_version_info() if v < (3, 0): default_hwm = 0 else: default_hwm = 1000 p, s = self.create_bound_pair(zmq.PUB, zmq.SUB) p.setsockopt(zmq.LINGER, 0) self.assertEqual(p.getsockopt(zmq.LINGER), 0) p.setsockopt(zmq.LINGER, -1) self.assertEqual(p.getsockopt(zmq.LINGER), -1) self.assertEqual(p.hwm, default_hwm) p.hwm = 11 self.assertEqual(p.hwm, 11) # p.setsockopt(zmq.EVENTS, zmq.POLLIN) self.assertEqual(p.getsockopt(zmq.EVENTS), zmq.POLLOUT) self.assertRaisesErrno(zmq.EINVAL, p.setsockopt, zmq.EVENTS, 2**7 - 1) self.assertEqual(p.getsockopt(zmq.TYPE), p.socket_type) self.assertEqual(p.getsockopt(zmq.TYPE), zmq.PUB) self.assertEqual(s.getsockopt(zmq.TYPE), s.socket_type) self.assertEqual(s.getsockopt(zmq.TYPE), zmq.SUB) # check for overflow / wrong type: errors = [] backref = {} constants = zmq.constants for name in constants.__all__: value = getattr(constants, name) if isinstance(value, int): backref[value] = name for opt in zmq.constants.int_sockopts.union( zmq.constants.int64_sockopts): sopt = backref[opt] if sopt.startswith(( 'ROUTER', 'XPUB', 'TCP', 'FAIL', 'REQ_', 'CURVE_', 'PROBE_ROUTER', 'IPC_FILTER', )): # some sockopts are write-only continue try: n = p.getsockopt(opt) except zmq.ZMQError as e: errors.append("getsockopt(zmq.%s) raised '%s'." % (sopt, e)) else: if n > 2**31: errors.append( "getsockopt(zmq.%s) returned a ridiculous value." " It is probably the wrong type." % sopt) if errors: self.fail('\n'.join([''] + errors))
def __init__(self, conf, ctx): if zmq.zmq_version_info() < (4, 0): raise RuntimeError( "Security is not supported in libzmq version < 4.0. libzmq version {0}" .format(zmq.zmq_version())) self._conf = conf self._zctx = ctx self._send_attempts = 0
def setUp(self): if zmq.zmq_version_info() < (4, 0): raise SkipTest("security is new in libzmq 4.0") super(TestThreadedAuthentication, self).setUp() # silence auth module debug log output during test runs logger = logging.getLogger() self.original_log_level = logger.getEffectiveLevel() logger.setLevel(logging.DEBUG) self.auth = None
def setUp(self): if zmq.zmq_version_info() < (4, 0): raise SkipTest("security is new in libzmq 4.0") try: zmq.curve_keypair() except zmq.ZMQError: raise SkipTest( "security requires libzmq to be built with CURVE support") super(TestSecurity, self).setUp()
def test_int_sockopts(self): "test integer sockopts" v = zmq.zmq_version_info() if v < (3,0): default_hwm = 0 else: default_hwm = 1000 p,s = self.create_bound_pair(zmq.PUB, zmq.SUB) p.setsockopt(zmq.LINGER, 0) self.assertEqual(p.getsockopt(zmq.LINGER), 0) p.setsockopt(zmq.LINGER, -1) self.assertEqual(p.getsockopt(zmq.LINGER), -1) self.assertEqual(p.hwm, default_hwm) p.hwm = 11 self.assertEqual(p.hwm, 11) # p.setsockopt(zmq.EVENTS, zmq.POLLIN) self.assertEqual(p.getsockopt(zmq.EVENTS), zmq.POLLOUT) self.assertRaisesErrno(zmq.EINVAL, p.setsockopt,zmq.EVENTS, 2**7-1) self.assertEqual(p.getsockopt(zmq.TYPE), p.socket_type) self.assertEqual(p.getsockopt(zmq.TYPE), zmq.PUB) self.assertEqual(s.getsockopt(zmq.TYPE), s.socket_type) self.assertEqual(s.getsockopt(zmq.TYPE), zmq.SUB) # check for overflow / wrong type: errors = [] backref = {} constants = zmq.constants for name in constants.__all__: value = getattr(constants, name) if isinstance(value, int): backref[value] = name for opt in zmq.constants.int_sockopts.union(zmq.constants.int64_sockopts): sopt = backref[opt] if sopt.startswith(( 'ROUTER', 'XPUB', 'TCP', 'FAIL', 'REQ_', 'CURVE_', 'PROBE_ROUTER', 'IPC_FILTER', 'GSSAPI', 'STREAM_', 'VMCI_BUFFER_SIZE', 'VMCI_BUFFER_MIN_SIZE', 'VMCI_BUFFER_MAX_SIZE', 'VMCI_CONNECT_TIMEOUT', )): # some sockopts are write-only continue try: n = p.getsockopt(opt) except zmq.ZMQError as e: errors.append("getsockopt(zmq.%s) raised '%s'."%(sopt, e)) else: if n > 2**31: errors.append("getsockopt(zmq.%s) returned a ridiculous value." " It is probably the wrong type."%sopt) if errors: self.fail('\n'.join([''] + errors))
def test_removed(self): zmq_version = zmq.zmq_version_info() for version, new_names in constant_names.removed_in.items(): should_have = zmq_version < version for name in new_names: try: value = getattr(zmq, name) except AttributeError: if should_have: self.fail("AttributeError: zmq.%s" % name) else: if not should_have: self.fail("Shouldn't have: zmq.%s=%s" % (name, value))
def test_int_sockopts(self): "test non-uint64 sockopts" v = zmq.zmq_version_info() if not v >= (2, 1): raise SkipTest("only on libzmq >= 2.1") elif v < (3, 0): hwm = zmq.HWM default_hwm = 0 else: hwm = zmq.SNDHWM default_hwm = 1000 p, s = self.create_bound_pair(zmq.PUB, zmq.SUB) p.setsockopt(zmq.LINGER, 0) self.assertEquals(p.getsockopt(zmq.LINGER), 0) p.setsockopt(zmq.LINGER, -1) self.assertEquals(p.getsockopt(zmq.LINGER), -1) self.assertEquals(p.getsockopt(hwm), default_hwm) p.setsockopt(hwm, 11) self.assertEquals(p.getsockopt(hwm), 11) # p.setsockopt(zmq.EVENTS, zmq.POLLIN) self.assertEquals(p.getsockopt(zmq.EVENTS), zmq.POLLOUT) self.assertRaisesErrno(zmq.EINVAL, p.setsockopt, zmq.EVENTS, 2 ** 7 - 1) self.assertEquals(p.getsockopt(zmq.TYPE), p.socket_type) self.assertEquals(p.getsockopt(zmq.TYPE), zmq.PUB) self.assertEquals(s.getsockopt(zmq.TYPE), s.socket_type) self.assertEquals(s.getsockopt(zmq.TYPE), zmq.SUB) # check for overflow / wrong type: errors = [] backref = {} constants = zmq.core.constants for name in constants.__all__: value = getattr(constants, name) if isinstance(value, int): backref[value] = name for opt in zmq.core.constants.int_sockopts + zmq.core.constants.int64_sockopts: sopt = backref[opt] if sopt == "FAIL_UNROUTABLE": # fail_unroutable is write-only continue try: n = p.getsockopt(opt) except zmq.ZMQError as e: errors.append("getsockopt(zmq.%s) raised '%s'." % (sopt, e)) else: if n > 2 ** 31: errors.append( "getsockopt(zmq.%s) returned a ridiculous value." " It is probably the wrong type." % sopt ) if errors: self.fail("\n".join(errors))
def test_int_sockopts(self): "test non-uint64 sockopts" v = zmq.zmq_version_info() if not v >= (2, 1): raise SkipTest("only on libzmq >= 2.1") elif v < (3, 0): hwm = zmq.HWM default_hwm = 0 else: hwm = zmq.SNDHWM default_hwm = 1000 p, s = self.create_bound_pair(zmq.PUB, zmq.SUB) p.setsockopt(zmq.LINGER, 0) self.assertEquals(p.getsockopt(zmq.LINGER), 0) p.setsockopt(zmq.LINGER, -1) self.assertEquals(p.getsockopt(zmq.LINGER), -1) self.assertEquals(p.getsockopt(hwm), default_hwm) p.setsockopt(hwm, 11) self.assertEquals(p.getsockopt(hwm), 11) # p.setsockopt(zmq.EVENTS, zmq.POLLIN) self.assertEquals(p.getsockopt(zmq.EVENTS), zmq.POLLOUT) self.assertRaisesErrno(zmq.EINVAL, p.setsockopt, zmq.EVENTS, 2**7 - 1) self.assertEquals(p.getsockopt(zmq.TYPE), p.socket_type) self.assertEquals(p.getsockopt(zmq.TYPE), zmq.PUB) self.assertEquals(s.getsockopt(zmq.TYPE), s.socket_type) self.assertEquals(s.getsockopt(zmq.TYPE), zmq.SUB) # check for overflow / wrong type: errors = [] backref = {} constants = zmq.core.constants for name in constants.__all__: value = getattr(constants, name) if isinstance(value, int): backref[value] = name for opt in zmq.core.constants.int_sockopts + zmq.core.constants.int64_sockopts: sopt = backref[opt] if sopt == 'ROUTER_BEHAVIOR' or 'TCP' in sopt: # fail_unroutable is write-only continue try: n = p.getsockopt(opt) except zmq.ZMQError as e: errors.append("getsockopt(zmq.%s) raised '%s'." % (sopt, e)) else: if n > 2**31: errors.append( "getsockopt(zmq.%s) returned a ridiculous value." " It is probably the wrong type." % sopt) if errors: self.fail('\n'.join([''] + errors))
def _check_version(min_version_info, msg='Feature'): """Check for libzmq raises ZMQVersionError if current zmq version is not at least min_version min_version_info is a tuple of integers, and will be compared against zmq.zmq_version_info(). """ global _zmq_version_info if _zmq_version_info is None: from zmq import zmq_version_info _zmq_version_info = zmq_version_info() if _zmq_version_info < min_version_info: min_version = '.'.join(str(v) for v in min_version_info) raise ZMQVersionError(min_version, msg)
def _select_recv(self, multipart, socket, **kwargs): """call recv[_multipart] in a way that raises if there is nothing to receive""" if zmq.zmq_version_info() >= (3, 1, 0): # zmq 3.1 has a bug, where poll can return false positives, # so we wait a little bit just in case # See LIBZMQ-280 on JIRA time.sleep(0.1) r, w, x = zmq.select([socket], [], [], timeout=5) assert len(r) > 0, "Should have received a message" kwargs['flags'] = zmq.DONTWAIT | kwargs.get('flags', 0) recv = socket.recv_multipart if multipart else socket.recv return recv(**kwargs)
def main(): if zmq.zmq_version_info() < (4, 0): raise RuntimeError( "Security is not supported in libzmq version < 4.0. libzmq version " "{0}".format(zmq.zmq_version())) if '-v' in sys.argv: level = logging.DEBUG else: level = logging.INFO logging.basicConfig(level=level, format="[%(levelname)s] %(message)s") run()
def _select_recv(self, multipart, socket, **kwargs): """call recv[_multipart] in a way that raises if there is nothing to receive""" if zmq.zmq_version_info() >= (3,1,0): # zmq 3.1 has a bug, where poll can return false positives, # so we wait a little bit just in case # See LIBZMQ-280 on JIRA time.sleep(0.1) r,w,x = zmq.select([socket], [], [], timeout=5) assert len(r) > 0, "Should have received a message" kwargs['flags'] = zmq.DONTWAIT | kwargs.get('flags', 0) recv = socket.recv_multipart if multipart else socket.recv return recv(**kwargs)
def test_proxy_bind_to_random_with_args(self): if zmq.zmq_version_info() < (3, 2): raise SkipTest("Proxies only in libzmq >= 3") dev = devices.ThreadProxy(zmq.PULL, zmq.PUSH, zmq.PUSH) iface = 'tcp://10.0.0.7' ports = [] min, max = 5000, 5050 ports.extend([ dev.bind_in_to_random_port(iface, min_port=min, max_port=max), dev.bind_out_to_random_port(iface, min_port=min, max_port=max), dev.bind_mon_to_random_port(iface, min_port=min, max_port=max) ]) for port in ports: if port < min or port > max: self.fail('Unexpected port number: %i' % port)
def __init__(self, context=None, encoding='utf-8'): if zmq.zmq_version_info() < (4,0): raise NotImplementedError("Security is only available in libzmq >= 4.0") self.context = context or zmq.Context.instance() self.encoding = encoding self.allow_any = False self.zap_socket = None self.whitelist = [] self.blacklist = [] # passwords is a dict keyed by domain and contains values # of dicts with username:password pairs. self.passwords = {} # certs is dict keyed by domain and contains values # of dicts keyed by the public keys from the specified location. self.certs = {}
def _setup_socket(self): if self.socket is None: self.socket = self.context.socket(self.socket_type) if self.routing_id: self.socket.identity = self.routing_id if self.socket_type == zmq.ROUTER: self.socket.ROUTER_MANDATORY = True if zmq.zmq_version_info() >= (4, 1, 0): self.socket.ROUTER_HANDOVER = True elif self.socket_type == zmq.REQ: self.socket.RCVTIMEO = int(self.timeout * 1000) self.socket.SNDTIMEO = int(self.timeout * 1000) self.auth_backend.configure() self.heartbeat_backend.configure() self.initialized = True
def test_proxy_bind_to_random_with_args(self): if zmq.zmq_version_info() < (3, 2): raise SkipTest("Proxies only in libzmq >= 3") dev = devices.ThreadProxy(zmq.PULL, zmq.PUSH, zmq.PUSH) iface = 'tcp://127.0.0.1' ports = [] min, max = 5000, 5050 ports.extend([ dev.bind_in_to_random_port(iface, min_port=min, max_port=max), dev.bind_out_to_random_port(iface, min_port=min, max_port=max), dev.bind_mon_to_random_port(iface, min_port=min, max_port=max) ]) for port in ports: if port < min or port > max: self.fail('Unexpected port number: %i' % port)
def test_hwm(self): zmq3 = zmq.zmq_version_info()[0] >= 3 for stype in (zmq.PUB, zmq.ROUTER, zmq.SUB, zmq.REQ, zmq.DEALER): s = self.context.socket(stype) s.hwm = 100 self.assertEqual(s.hwm, 100) if zmq3: try: self.assertEqual(s.sndhwm, 100) except AttributeError: pass try: self.assertEqual(s.rcvhwm, 100) except AttributeError: pass s.close()
def get_hwm(self): """get the High Water Mark On libzmq ≥ 3.x, this gets SNDHWM if available, otherwise RCVHWM """ major = zmq.zmq_version_info()[0] if major >= 3: # return sndhwm, fallback on rcvhwm try: return self.getsockopt(zmq.SNDHWM) except zmq.ZMQError as e: pass return self.getsockopt(zmq.RCVHWM) else: return self.getsockopt(zmq.HWM)
def _setup_socket(self, probing=False): if self.socket is None: self.socket = self.context.socket(self.socket_type) if self.routing_id: self.socket.setsockopt(zmq.IDENTITY, self.routing_id) if self.socket_type == zmq.ROUTER: self.socket.setsockopt(zmq.ROUTER_MANDATORY, True) if zmq.zmq_version_info() >= (4, 1, 0): self.socket.setsockopt(zmq.ROUTER_HANDOVER, True) elif self.socket_type == zmq.REQ: self.socket.setsockopt(zmq.RCVTIMEO, int(self.timeout * 1000)) self.socket.setsockopt(zmq.SNDTIMEO, int(self.timeout * 1000)) self.socket.setsockopt(zmq.PROBE_ROUTER, probing) self.auth_backend.configure() self.heartbeat_backend.configure() self.initialized = True
def test_proxy_steerable_bind_to_random_with_args(self): if zmq.zmq_version_info() < (4, 1): raise SkipTest("Steerable Proxies only in libzmq >= 4.1") dev = devices.ThreadProxySteerable(zmq.PULL, zmq.PUSH, zmq.PUSH, zmq.PAIR) iface = 'tcp://127.0.0.1' ports = [] min, max = 5000, 5050 ports.extend([ dev.bind_in_to_random_port(iface, min_port=min, max_port=max), dev.bind_out_to_random_port(iface, min_port=min, max_port=max), dev.bind_mon_to_random_port(iface, min_port=min, max_port=max), dev.bind_ctrl_to_random_port(iface, min_port=min, max_port=max), ]) for port in ports: if port < min or port > max: self.fail('Unexpected port number: %i' % port)
def connect_or_bind(self, name, endpoint): if self.socket is None: self.socket = self.context.socket(self.socket_type) if self.routing_id: self.socket.identity = self.routing_id if self.socket_type == zmq.ROUTER: self.socket.ROUTER_MANDATORY = True if zmq.zmq_version_info() >= (4, 1, 0): self.socket.ROUTER_HANDOVER = True elif self.socket_type == zmq.REQ: self.socket.RCVTIMEO = int(self.timeout * 1000) self.socket.SNDTIMEO = int(self.timeout * 1000) self.auth_backend.configure() self.heartbeat_backend.configure() caller = operator.methodcaller(name, endpoint) caller(self.socket) self.initialized = True
def __init__(self, context=None, encoding='utf-8', log=None): if zmq.zmq_version_info() < (4, 0): raise NotImplementedError( "Security is only available in libzmq >= 4.0") self.context = context or zmq.Context.instance() self.encoding = encoding self.allow_any = False self.zap_socket = None self.whitelist = set() self.blacklist = set() # passwords is a dict keyed by domain and contains values # of dicts with username:password pairs. self.passwords = {} # certs is dict keyed by domain and contains values # of dicts keyed by the public keys from the specified location. self.certs = {} self.log = log or logging.getLogger('zmq.auth')
def set_tcp_keepalive(sock, opts=None): # Warn if ZMQ < 3.2 try: zmq_version_info = zmq.zmq_version_info() except AttributeError: # PyZMQ <= 2.1.9 does not have zmq_version_info, fall back to # using zmq.zmq_version() and build a version info tuple. zmq_version_info = tuple( [int(x) for x in zmq.zmq_version().split('.')] ) if zmq_version_info < (3, 2): log.warning( 'You have a version of ZMQ less than ZMQ 3.2! There are ' 'known connection keep-alive issues with ZMQ < 3.2 which ' 'may result in loss of contact with minions. Please ' 'upgrade your ZMQ!' ) #### # try: # ip=socket.gethostbyname(socket.gethostname()) # if ip == "127.0.0.1": # raise Exception("127.0.0.1") # except Exception,e: # (status,ip) = commands.getstatusoutput("/sbin/ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}'|tr -d 'addr:'") # ip = ip if opts['ha'] == True: while True: time.sleep(5) try: sys.path.append("/usr/lib/python2.7/site-packages/salt") from zkclient import ZKClient, zookeeper, watchmethod zk = ZKClient('172.16.163.14:2181',1000) if zk.exists('/broker'): value = zk.get('/broker') logging.error(str(value[0])) if value[0] == opts['id']: break else: zk.create('/broker') zk.set('/broker',data='%s'%opts['id']) logging.error("create the zk path is successful !") continue except Exception,e: logging.error(str(e)) continue
def test_server_can_send_to_trustable_peer_identity(self): """ Uses internal metadata of zmq.Frame.get() to fetch identity of sender """ from pseud import Client, Server server_id = b'server' endpoint = b'tcp://127.0.0.1:8989' server_public, server_secret = zmq.curve_keypair() security_plugin = 'trusted_curve' server = Server(server_id, security_plugin=security_plugin, public_key=server_public, secret_key=server_secret, io_loop=self.io_loop) server.bind(endpoint) bob_public, bob_secret = server.auth_backend.known_identities[b'bob'] client = Client(server_id, user_id=b'bob', security_plugin=security_plugin, public_key=bob_public, secret_key=bob_secret, peer_public_key=server_public, io_loop=self.io_loop) client.connect(endpoint) assert server.socket.mechanism == zmq.CURVE assert client.socket.mechanism == zmq.CURVE yield server.start() yield client.start() @server.register_rpc(with_identity=True) def echo(peer_identity, message): return peer_identity, message result = yield client.echo(b'one') if zmq.zmq_version_info() >= (4, 1, 0): assert result == (b'bob', b'one') else: assert result == (b'', b'one') server.stop() client.stop()
def setUp(self): super(TestIOLoopAuthentication, self).setUp() if zmq.zmq_version_info() < (4,0): raise SkipTest("security is new in libzmq 4.0") # silence auth module debug log output during test runs logger = logging.getLogger() self.original_log_level = logger.getEffectiveLevel() logger.setLevel(logging.DEBUG) self.test_result = True self.io_loop = ioloop.IOLoop() self.auth = None self.server = self.socket(zmq.PUSH) self.client = self.socket(zmq.PULL) # Only need slow reconnect intervals for testing. self.client.reconnect_ivl = 1000 self.pushstream = zmqstream.ZMQStream(self.server, self.io_loop) self.pullstream = zmqstream.ZMQStream(self.client, self.io_loop)
def __init__(self, opts): ''' Create a salt broker instance ''' self.opts = opts # Warn if ZMQ < 3.2 try: zmq_version_info = zmq.zmq_version_info() except AttributeError: # PyZMQ <= 2.1.9 does not have zmq_version_info, fall back to # using zmq.zmq_version() and build a version info tuple. zmq_version_info = tuple( [int(x) for x in zmq.zmq_version().split('.')]) if zmq_version_info < (3, 2): log.warning( 'You have a version of ZMQ less than ZMQ 3.2! There are ' 'known connection keep-alive issues with ZMQ < 3.2 which ' 'may result in loss of contact with minions. Please ' 'upgrade your ZMQ!')
def get_monitor_socket(self, events=None, addr=None): """Return a connected PAIR socket ready to receive the event notifications. .. versionadded:: libzmq-4.0 .. versionadded:: 14.0 Parameters ---------- events : int [default: ZMQ_EVENTS_ALL] The bitmask defining which events are wanted. addr : string [default: None] The optional endpoint for the monitoring sockets. Returns ------- socket : (PAIR) The socket is already connected and ready to receive messages. """ # safe-guard, method only available on libzmq >= 4 if zmq.zmq_version_info() < (4, ): raise NotImplementedError( "get_monitor_socket requires libzmq >= 4, have %s" % zmq.zmq_version()) # if already monitoring, return existing socket if self._monitor_socket: if self._monitor_socket.closed: self._monitor_socket = None else: return self._monitor_socket if addr is None: # create endpoint name from internal fd addr = "inproc://monitor.s-%d" % self.FD if events is None: # use all events events = zmq.EVENT_ALL # attach monitoring socket self.monitor(addr, events) # create new PAIR socket and connect it self._monitor_socket = self.context.socket(zmq.PAIR) self._monitor_socket.connect(addr) return self._monitor_socket
def test_router_router(self): """test router-router MQ devices""" dev = devices.ThreadMonitoredQueue(zmq.ROUTER, zmq.ROUTER, zmq.PUB, b'in', b'out') self.device = dev dev.setsockopt_in(zmq.LINGER, 0) dev.setsockopt_out(zmq.LINGER, 0) dev.setsockopt_mon(zmq.LINGER, 0) binder = self.context.socket(zmq.DEALER) porta = binder.bind_to_random_port('tcp://127.0.0.1') portb = binder.bind_to_random_port('tcp://127.0.0.1') binder.close() time.sleep(0.1) a = self.context.socket(zmq.DEALER) a.identity = b'a' b = self.context.socket(zmq.DEALER) b.identity = b'b' self.sockets.extend([a, b]) a.connect('tcp://127.0.0.1:%i' % porta) dev.bind_in('tcp://127.0.0.1:%i' % porta) b.connect('tcp://127.0.0.1:%i' % portb) dev.bind_out('tcp://127.0.0.1:%i' % portb) dev.start() time.sleep(0.2) if zmq.zmq_version_info() >= (3, 1, 0): # flush erroneous poll state, due to LIBZMQ-280 ping_msg = [b'ping', b'pong'] for s in (a, b): s.send_multipart(ping_msg) try: s.recv(zmq.NOBLOCK) except zmq.ZMQError: pass msg = [b'hello', b'there'] a.send_multipart([b'b'] + msg) bmsg = self.recv_multipart(b) self.assertEqual(bmsg, [b'a'] + msg) b.send_multipart(bmsg) amsg = self.recv_multipart(a) self.assertEqual(amsg, [b'b'] + msg) self.teardown_device()
def open(self): if self._socket and not self._socket.closed: self.close() self._socket = self._context.socket(self._qtype) if zmq.zmq_version_info()[0] == 2: self._socket.setsockopt(zmq.HWM, self._hwm) else: self._socket.setsockopt(zmq.SNDHWM, self._hwm) self._socket.setsockopt(zmq.RCVHWM, self._hwm) if not self._attach: if self._qtype in (zmq.PUB, zmq.REP, zmq.PUSH): self._attach = BIND elif self._qtype in (zmq.SUB, zmq.REQ, zmq.PULL): self._attach = CONNECT for a in self._addr: if self._attach == BIND: self._socket.bind(a) log.debug('bind: %s HWM=%d' % (a, self._hwm)) time.sleep(0.5) # HACK: need proper startup here elif self._attach == CONNECT: if self._ssh_key and self._ssh_conn: zmq.ssh.tunnel_connection(self._socket, a, server=self._ssh_conn, keyfile=self._ssh_key, paramiko=self._use_paramiko) log.debug('tunnel_connection: %s HWM=%d, server=%s' % (a, self._hwm, self._ssh_conn)) else: self._socket.connect(a) log.debug('connect: %s HWM=%d' % (a, self._hwm)) else: raise Exception( 'Invalid attachment type %s. Use BIND or CONNECT.' % (str(self._attach), )) if self._qtype == zmq.SUB: self._socket.setsockopt(zmq.SUBSCRIBE, '')
def test_router_router(self): """test router-router MQ devices""" dev = devices.ThreadMonitoredQueue(zmq.ROUTER, zmq.ROUTER, zmq.PUB, b'in', b'out') self.device = dev dev.setsockopt_in(zmq.LINGER, 0) dev.setsockopt_out(zmq.LINGER, 0) dev.setsockopt_mon(zmq.LINGER, 0) binder = self.context.socket(zmq.DEALER) porta = binder.bind_to_random_port('tcp://127.0.0.1') portb = binder.bind_to_random_port('tcp://127.0.0.1') binder.close() time.sleep(1) a = self.context.socket(zmq.DEALER) a.identity = b'a' b = self.context.socket(zmq.DEALER) b.identity = b'b' self.sockets.extend([a, b]) a.connect('tcp://127.0.0.1:%i'%porta) dev.bind_in('tcp://127.0.0.1:%i'%porta) b.connect('tcp://127.0.0.1:%i'%portb) dev.bind_out('tcp://127.0.0.1:%i'%portb) dev.start() time.sleep(1) if zmq.zmq_version_info() >= (3,1,0): # flush erroneous poll state, due to LIBZMQ-280 ping_msg = [ b'ping', b'pong' ] for s in (a,b): s.send_multipart(ping_msg) try: s.recv(zmq.NOBLOCK) except zmq.ZMQError: pass msg = [ b'hello', b'there' ] a.send_multipart([b'b']+msg) bmsg = self.recv_multipart(b) self.assertEqual(bmsg, [b'a']+msg) b.send_multipart(bmsg) amsg = self.recv_multipart(a) self.assertEqual(amsg, [b'b']+msg) self.teardown_device()
def monitor(self, addr, events=-1): """s.monitor(addr, flags) Start publishing socket events on inproc. See libzmq docs for zmq_monitor for details. Note: requires libzmq >= 3.2 Parameters ---------- addr : str The inproc url used for monitoring. events : int [default: zmq.EVENT_ALL] The zmq event bitmask for which events will be sent to the monitor. """ if zmq.zmq_version_info() < (3,2): raise NotImplementedError("monitor requires libzmq >= 3.2, have %s" % zmq.zmq_version()) if events < 0: events = zmq.EVENT_ALL rc = C.zmq_socket_monitor(self._zmq_socket, addr, events)
def test_proxy_steerable_statistics(self): if zmq.zmq_version_info() < (4, 3): raise SkipTest("STATISTICS only in libzmq >= 4.3") dev = devices.ThreadProxySteerable(zmq.PULL, zmq.PUSH, zmq.PUSH, zmq.PAIR) binder = self.context.socket(zmq.REQ) iface = 'tcp://127.0.0.1' port = binder.bind_to_random_port(iface) port2 = binder.bind_to_random_port(iface) port3 = binder.bind_to_random_port(iface) port4 = binder.bind_to_random_port(iface) binder.close() time.sleep(0.1) dev.bind_in("%s:%i" % (iface, port)) dev.bind_out("%s:%i" % (iface, port2)) dev.bind_mon("%s:%i" % (iface, port3)) dev.bind_ctrl("%s:%i" % (iface, port4)) dev.start() time.sleep(0.25) msg = b'hello' push = self.context.socket(zmq.PUSH) push.connect("%s:%i" % (iface, port)) pull = self.context.socket(zmq.PULL) pull.connect("%s:%i" % (iface, port2)) mon = self.context.socket(zmq.PULL) mon.connect("%s:%i" % (iface, port3)) ctrl = self.context.socket(zmq.PAIR) ctrl.connect("%s:%i" % (iface, port4)) push.send(msg) self.sockets.extend([push, pull, mon, ctrl]) self.assertEqual(msg, self.recv(pull)) self.assertEqual(msg, self.recv(mon)) ctrl.send(b'STATISTICS') stats = self.recv_multipart(ctrl) stats_int = [struct.unpack("<Q", x)[0] for x in stats] self.assertEqual(1, stats_int[0]) self.assertEqual(len(msg), stats_int[1]) self.assertEqual(1, stats_int[6]) self.assertEqual(len(msg), stats_int[7]) ctrl.send(b'TERMINATE') dev.join()
def test_curve_public(self): """test curve_public""" try: public, secret = zmq.curve_keypair() except zmq.ZMQError: raise SkipTest("CURVE unsupported") if zmq.zmq_version_info() < (4, 2): raise SkipTest("curve_public is new in libzmq 4.2") derived_public = zmq.curve_public(secret) self.assertEqual(type(derived_public), bytes) self.assertEqual(len(derived_public), 40) # verify that it is indeed Z85 bpublic = z85.decode(derived_public) self.assertEqual(type(bpublic), bytes) self.assertEqual(len(bpublic), 32) # verify that it is equal to the known public key self.assertEqual(derived_public, public)
def test_proxy(self): if zmq.zmq_version_info() < (3, 2): raise SkipTest("Proxies only in libzmq >= 3") dev = devices.ThreadProxy(zmq.PULL, zmq.PUSH, zmq.PUSH) iface = 'tcp://10.0.0.7' port = dev.bind_in_to_random_port(iface) port2 = dev.bind_out_to_random_port(iface) port3 = dev.bind_mon_to_random_port(iface) dev.start() time.sleep(0.25) msg = b'hello' push = self.context.socket(zmq.PUSH) push.connect("%s:%i" % (iface, port)) pull = self.context.socket(zmq.PULL) pull.connect("%s:%i" % (iface, port2)) mon = self.context.socket(zmq.PULL) mon.connect("%s:%i" % (iface, port3)) push.send(msg) self.sockets.extend([push, pull, mon]) self.assertEqual(msg, self.recv(pull)) self.assertEqual(msg, self.recv(mon))
def set_hwm(self, value): """set the High Water Mark On libzmq ≥ 3.x, this sets *both* SNDHWM and RCVHWM """ major = zmq.zmq_version_info()[0] if major >= 3: raised = None try: self.sndhwm = value except Exception as e: raised = e try: self.rcvhwm = value except Exception: raised = e if raised: raise raised else: return self.setsockopt(zmq.HWM, value)
def __init__(self, broker, conf=None, context=None, io_loop = None): if zmq.zmq_version_info() < (4, 0): raise RuntimeError("monitoring in libzmq version < 4.0 is not supported") conf = conf or {} self._conf = dict( autostart=conf.get('autostart') or False, reconnect=conf.get('reconnect') or 1000, heartbeat=conf.get('heartbeat') or 2500, timeout=conf.get('timeout') or 60000, retry=0, prefix='C{0}'.format(uuid4()) ) print('client id {0}'.format( self._conf.get('prefix'))) self._liveness = HEARTBEAT_LIVENESS self._broker = broker self._loop = io_loop or ioloop.IOLoop.current() self._ctx = context or zmq.Context.instance() self._reqs = dict() if self._conf['autostart']: self.start()
def zmq_has_curve(): """ Return whether the current ZMQ has support for auth and CurveZMQ security. :rtype: bool Version notes: `zmq.curve_keypair()` is new in version 14.0, new in version libzmq-4.0. Requires libzmq (>= 4.0) to have been linked with libsodium. `zmq.auth` module is new in version 14.1 `zmq.has()` is new in version 14.1, new in version libzmq-4.1. """ if platform.system() == "Windows": # TODO: curve is not working on windows #7919 return False zmq_version = zmq.zmq_version_info() pyzmq_version = zmq.pyzmq_version_info() if pyzmq_version >= (14, 1, 0) and zmq_version >= (4, 1): return zmq.has('curve') if pyzmq_version < (14, 1, 0): return False if zmq_version < (4, 0): # security is new in libzmq 4.0 return False try: zmq.curve_keypair() except zmq.error.ZMQError: # security requires libzmq to be linked against libsodium return False return True
import zlib import os import time from tempfile import mkstemp import six from six.moves import cPickle as pickle import snappy from zmq import constants, error, Socket, ZMQError from zope.interface import implementer from twisted.internet import reactor from twisted.internet.interfaces import IFileDescriptor, IReadDescriptor from twisted.python import log from zmq import zmq_version_info ZMQ3 = zmq_version_info()[0] >= 3 from veles.txzmq.manager import ZmqContextManager from veles.txzmq.sharedio import SharedIO from veles.compat import lzma, from_none from veles.logger import Logger from veles.pickle2 import best_protocol class ZmqEndpointType(object): """ Endpoint could be "bound" or "connected". """ bind = "bind" """