0

我需要一些帮助,我试图在 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
4

1 回答 1

1

打电话tcpdecode();只需添加以下行:

tcpdecode()

在模块的底部。

于 2013-09-19T18:47:26.953 回答