我需要一些帮助,我试图打破并在Python中使用IP标头,我编写了代码,但是我没有在屏幕上得到任何回复。我不知道我错过了什么。欢迎提出建议:
import struct
from collections import OrderedDict
def tcpheader (rawTCP):
#Using dict for the dictionary
tcp=OrderedDict.fromkeys({})
tcp['ttl']=struct.unpack('!H',rawTCP[8:12]) [0] # H! is to unpack the hex file and then search for the portion specified
tcp['totalength']=struct.unpack('!H',rawTCP[0:20]) [0]
tcp['sourceip']=struct.unpack('!H',rawTCP[12:16]) [0]
tcp['destip']=struct.unpack('!H',rawTCP[16:20]) [0]
return tcp #this is the proper way to break the loop and return
def tcpdecode():
infile=open ('C:/users/jwattenbarger/desktop/ip.dd', 'rb') #this is opening the ip.dd file
rawTCP=infile.read(40)
#IPraw=infile.read (40)
tcp=tcpheader(rawTCP)
#tcp=tcpheader(IPraw)
print ("ttl:", tcp['ttl']) #these are printing the data
print ("totalength:", tcp['totalength'])
print ("sourceip:", tcp['sourceip'])
print ("destip:", tcp['destip'])
Print ("Temp")
for i in tcp: #associating i with tcp
print (i, "\t",tcp[i]) #printing tcp to the screen in a table
答案 0 :(得分:1)
致电tcpdecode()
;只需添加以下行:
tcpdecode()
位于模块底部。