python 抓包

import pcap, dpkt, struct
import binascii

def main():
    a = pcap.pcap()
    a.setfilter('udp portrange 4000-4050')

    try:
        for i,pdata in a:
            p=dpkt.ethernet.Ethernet(pdata)
            src='%d.%d.%d.%d' % tuple(map(ord,list(p.data.src)))
            dst='%d.%d.%d.%d' % tuple(map(ord,list(p.data.dst)))
            sport = p.data.data.sport
            dport = p.data.data.dport
            qq = int( binascii.hexlify(p.data.data.data[7:11]) , 16 )
            print 'QQ: %d, From: %s:%d , To: %s:%d' % (qq,src,sport,dst,dport)

    except Exception,e:

        print '%s' % e

    n = raw_input()


if __name__ == '__main__':
    main()

 

你可能感兴趣的:(python)