def _decode_kv(cls, list): kv = {} prev = None for i in range(0, len(list), 2): key = bytes_to_str(list[i]) if i > 0 and key < prev: raise 'k/v keys are not sorted' kv[key] = list[i+1] prev = key return kv
def __init__(self, ip, udp_port, tcp_port=0, from_binary=False): tcp_port = tcp_port or udp_port if from_binary: self.udp_port = dec_port(udp_port) self.tcp_port = dec_port(tcp_port) else: assert is_integer(udp_port) assert is_integer(tcp_port) self.udp_port = udp_port self.tcp_port = tcp_port try: # `ip` could be in binary or ascii format, independent of # from_binary's truthy. We use ad-hoc regexp to determine format _ip = str_to_bytes(ip) _ip = (bytes_to_str(ip) if PY3 else unicode(ip)) if ip_pattern.match(_ip) else _ip self._ip = ipaddress.ip_address(_ip) except ipaddress.AddressValueError as e: log.debug("failed to parse ip", error=e, ip=ip) raise e
def host_port_pubkey_to_uri(host, port, pubkey): assert len(pubkey) == 512 // 8 uri = '{}{}@{}:{}'.format(node_uri_scheme, bytes_to_str(encode_hex(pubkey)), str(host), port) return str_to_bytes(uri)