Packet sniffing in Python (Windows)

前端 未结 6 1388
悲&欢浪女
悲&欢浪女 2020-12-01 03:02

What is the best way to sniff network packets using Python?

I\'ve heard from several places that the best module for this is a module called Scapy, unfortunately, it

6条回答
  •  时光说笑
    2020-12-01 03:45

    Use python-libpcap.

    import pcap
    
    p = pcap.pcapObject()
    dev = pcap.lookupdev()
    p.open_live(dev, 1600, 0, 100)
    #p.setnonblock(1)
    try:
        for pktlen, data, timestamp in p:
            print "[%s] Got data: %s" % (time.strftime('%H:%M', 
                                                       time.localtime(timestamp)),
                                         data)
    except KeyboardInterrupt:
        print '%s' % sys.exc_type
        print 'shutting down'
        print ('%d packets received, %d packets dropped'
               ' %d packets dropped by interface') % p.stats()
    

提交回复
热议问题