How to pass data between def - python -
i unsure if going right way advice ace !
basically have code :
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 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 version cell's payload def buildversions(acceptversions): pkt = '' v in acceptversions: pkt += struct.pack(">h", v) return pkt verpl = buildversions([ 3 ]) vercell = buildcell(0, 7, verpl) print "packet send : ", 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]
which sends packet, , recieves it,
what trying focus on decodnetinfo bit
what want here call how , retrieve pl
recvcell
function if packet recieved cmd ==8
how can pass data recvcell decodnetinfo ?
thanks
edit::
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
i have done not 2 return statements decode netinfo ideas? thanks
the return of recvcell() following dictionary
return { 'circid': circid, 'cmd': cmd, 'len': ln, 'pl': pl}
so after call function, pass key dictionary value
srv_netinfocell = recvcell(ssl_sock, 8) decodnetinfo(srv_netinfocell['pl'])
Comments
Post a Comment