Monitor your Browsing/Downloading data via Python


So here’s a small script which will monitor the amount of your browsing/downloading data.
I have used pypcap and dpkt library. dpkt is a library which provides packets creation/ parsing capabilities with an object oriented interface. The project is hosted at .

import pcap, dpkt, socket

pc = pcap.pcap()
ports = (80, 8080, 443, 888) # for HTPP and HTPPS

pc is now my pcap.pcap object. pcap objects are their own iterator returning the timestamp and
the packet as a 2-tuple.

def process():
  mem = sport = dport = 0
    for ts, pkt in pc:
      eth = dpkt.ethernet.Ethernet(pkt)
      ip =

now ts contains timestamp and data is stored in pkt
eth is an object of class dpkt.ethernet.Ethernet
Now accessing ip <class ‘dpkt.ip.IP’>, we access the data as

      if ip.__class__ == dpkt.ip.IP:
        ip1, ip2 = map(socket.inet_ntoa, [ip.src, ip.dst])
        if ip.p == socket.IPPROTO_TCP:
          I7 =
          sport, dport = [, I7.dport]

We use socket.inet_ntoa mapping to get the source IP address and Destination IP address. contains all the required data information that we need.

          if sport in ports or dport in ports:
            if len( > 0:
              print 'From %s to %s, length: %d' %(ip1, ip2, len(
              mem = mem + len(
    except KeyboardInterrupt:
      return int(mem)

len( is the amount of data(in bytes) that package (pkt) contains.
And to wrap it up,

def main():
  mem = process()
  print float(mem/(1024*1024)), 'mb'

if __name__ == '__main__':

I hope you find this code helpful. Suggestions are welcome.

So, A day well spent. Learned a bit about networking, ports, pypcap, dpkt!
P.S. Python seems fun!

3 thoughts on “Monitor your Browsing/Downloading data via Python

  1. Pingback: Quora

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s