时间:2021-07-01 10:21:17 帮助过:19人阅读
def decodeNodes(nodes): """ 把收到的nodes转成例表. 数据格式: [ (node ID, ip, port),(node ID, ip, port),(node ID, ip, port).... ] """ n = [] nrnodes = len(nodes) / 26 nodes = unpack("!" + "20sIH" * nrnodes, nodes) for i in xrange(nrnodes): nid, ip, port = nodes[i * 3], numToDottedQuad(nodes[i * 3 + 1]), nodes[i * 3 + 2] print nid n.append((nid, ip, port)) return ndef encodeNodes(nodes): """ "编码"nodes 与decodeNodes相反 """ n = [] for node in nodes: n.extend([node.nid, dottedQuadToNum(node.ip), node.port]) return pack("!" + "20sIH" * len(nodes), *n)
20字节的node id : a20 或 A20 如果是用空格充填不足就用后者
4字节的ip: L ip 地址用 ip2long 转换成无符号长整形数
2字节的端口: S
打包
$s = pack('a20LS', $nodeid, ip2logn($ip), 456);
解包
$d = unpack('a20nodeid/Lip/Sport', $s);
$d['ip'] = logn2ip($d['ip']);
print_r($d); //就清楚了
20字节的node id : a20 或 A20 如果是用空格充填不足就用后者
4字节的ip: L ip 地址用 ip2long 转换成无符号长整形数
2字节的端口: S
打包
$s = pack('a20LS', $nodeid, ip2logn($ip), 456);
解包
$d = unpack('a20nodeid/Lip/Sport', $s);
$d['ip'] = logn2ip($d['ip']);
print_r($d); //就清楚了