使用 python 监控我的下载使用情况

发布于 2024-12-26 05:45:32 字数 196 浏览 1 评论 0原文

我想用 python 编写一个程序来监视我的浏览和下载数据,并显示我在一天(或在给定的时间间隔内)下载了多少数据。

我不想要我下载的内容,我只需要知道我下载和/或浏览了多少数据(数据量)。

为此我应该访问端口还是其他什么?
我应该如何进行?

编辑:我使用的是 Ubuntu 11.10。

I want to make a program in python which monitors my browsing and downloading data and shows me how much data I have downloaded in a day (or in a given time interval).

I don't want what I have downloaded, I just need to know how much data (the amount of data) that I have downloaded and/or browsed.

For that should I access the ports or something else?
How should I proceed ?

Edit: I am using Ubuntu 11.10.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

巷子口的你 2025-01-02 05:45:32

我对如何监控我的下载有了基本的了解。我基本上获取数据包的数据并将其相加,

    #!/usr/bin/python2.7
    import pcap, dpkt, socket

    pc = pcap.pcap()
    ports = (80, 8080, 888, 443)

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

          if ip.__class__ == dpkt.ip.IP:
            ip1, ip2 = map(socket.inet_ntoa, [ip.src, ip.dst])
            if ip.p == 6:
              I7 = ip.data
              sport, dport = [I7.sport, I7.dport]
            if sport in ports or dport in ports:
              if len(data) > 0:
                print 'From %s to %s, length: %d' %(ip1, ip2, len(I7.data))
                mem = mem + len(I7.data)
     except KeyboardInterrupt:
       return mem

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

    if __name__ == '__main__':
      main()

这会打印源 ip、目标 ip、包的长度,直到提供 keybaordinterrupt。

I got a basic idea of how to monitor my downloads. I basically take the data of packets and add it up

    #!/usr/bin/python2.7
    import pcap, dpkt, socket

    pc = pcap.pcap()
    ports = (80, 8080, 888, 443)

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

          if ip.__class__ == dpkt.ip.IP:
            ip1, ip2 = map(socket.inet_ntoa, [ip.src, ip.dst])
            if ip.p == 6:
              I7 = ip.data
              sport, dport = [I7.sport, I7.dport]
            if sport in ports or dport in ports:
              if len(data) > 0:
                print 'From %s to %s, length: %d' %(ip1, ip2, len(I7.data))
                mem = mem + len(I7.data)
     except KeyboardInterrupt:
       return mem

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

    if __name__ == '__main__':
      main()

This prints source ip, dest ip, length of package until keybaordinterrupt is provided.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文