如何在def-python之间传递数据

时间:2014-06-08 21:02:05

标签: python

我不确定我是否会采用正确的方式,所以任何建议都是王牌!

基本上我有这个代码:

def recvCell(sock, waitFor = 0):
    while True:
        hdr = sock.recv(3)
        circid, cmd = struct.unpack(">HB", hdr[0:3])
        ln = 509
        if cmd == 7 or cmd >= 128:
            ln = struct.unpack(">H", sock.recv(2))[0]
        pl = sock.recv(ln)

        if cmd == waitFor or waitFor == 0:
            return { 'circId': circid, 'cmd': cmd, 'len': ln, 'pl': pl}

# builds a cell
def buildCell(circid, command, payload):
    cell = struct.pack(">HB", circid, command)
    if command == 7 or command >= 128:
        cell += struct.pack(">H", len(payload))
    else:
        payload += "\x00" * (509 - len(payload))

    cell += payload
    return cell

# builds the version cell's payload
def buildVersions(acceptVersions):
    pkt = ''
    for v in acceptVersions:
        pkt += struct.pack(">H", v)
    return pkt

verPl = buildVersions([ 3 ])
verCell = buildCell(0, 7, verPl)

print "Packet to send is : ", verCell
ssl_sock.send(verCell)

srv_netinfocell = recvCell(ssl_sock, 8)
#process netinfo cell here
print srv_netinfocell

def decodNetInfo(pl):
    payload = pl
    tm = struct.unpack(">L", payload[0:4])
    our_or_ip_version = struct.unpack(">B", payload[4])[0]
    our_or_addr_len = struct.unpack(">B", payload[5])[0] 
    return{}
        if our_or_addr_len == 4:
        our_op_ip = struct.unpack(">BBBB", payload[6:10]) 
        our_ip_version = 4
        num_their_ips = struct.unpack(">B", payload[10])[0]
        len_their_ips = struct.unpack(">b", payload[12][0]

发送数据包并收到它,

我想要关注的是decodNetinfo位

我想在这里做的就是调用一些方法并从pl函数中检索recvCell如果收到的数据包是cmd == 8

如何将这些数据从recvCell传递到decodNetInfo?

感谢

编辑::

def recvCell(sock, waitFor = 0):
    while True:
        hdr = sock.recv(3)
        circid, cmd = struct.unpack(">HB", hdr[0:3])
        ln = 509
        if cmd == 7 or cmd >= 128:
            ln = struct.unpack(">H", sock.recv(2))[0]
        pl = sock.recv(ln)

        if cmd == waitFor or waitFor == 0:
            return { 'circId': circid, 'cmd': cmd, 'len': ln, 'pl': pl}

def decodeNetInfo(pl):
    return  "in decode net info"
    return { 'pl': pl}


srv_netinfocell = recvCell(ssl_sock, 8)
decodeNetInfo(srv_netinfocell['pl'])
#process netinfo cell here
print srv_netinfocell

我已经这样做但是没有从解码网络信息获得两个返回语句的任何想法?感谢

1 个答案:

答案 0 :(得分:1)

recvCell()的返回是以下字典

return { 'circId': circid, 'cmd': cmd, 'len': ln, 'pl': pl}

因此,在调用该函数后,只需将该密钥传递给字典即可获得值

srv_netinfocell = recvCell(ssl_sock, 8)
decodNetInfo(srv_netinfocell['pl'])