Hi..
I'm just curious as to how one would set about writing some python code to monitor the network usage in one session...
The program should simply record how much data has been downloaded from the time the computer is switched on to the time it was switched off.
What are the modules (if any) that could be used?

(pls don't provide any actual code... I'd like to try it on my own someday.. Just give me some pointers ;-)
Thanks

Hello, I'm pretty new to network programming as well. But I have managed to get some simple codes working such as a sniffer which can moniter all packets traveling in and out. I would assume that something like this would be useful so maybe read up as much as you can about socket programming.

Hello, I'm pretty new to network programming as well. But I have managed to get some simple codes working such as a sniffer which can moniter all packets traveling in and out. I would assume that something like this would be useful so maybe read up as much as you can about socket programming.

Can you post the sniffer's code ?

what are the modules you used?

Hey guys, yeah it's basically just an example I pulled from the library, it uses the socket module to examine packets at the socket layer. I'm running it on Vista with admin privliages.

import socket

# the public network interface
HOST = socket.gethostbyname(socket.gethostname())

# create a raw socket and bind it to the public interface
s = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_IP)
s.bind((HOST, 0))

while True:
    
    # Include IP headers
    s.setsockopt(socket.IPPROTO_IP, socket.IP_HDRINCL, 1)

    # receive all packages
    s.ioctl(socket.SIO_RCVALL, socket.RCVALL_ON)

    # receive a package
    newfile = open("results.txt", "at")
    packets = (s.recvfrom(65565)[0],"hey", s.recvfrom(65565)[1])
    newfile.write(str(packets[0]))
    newfile.write(str(packets[1]))
    newfile.write("\n")
    newfile.close()
    print (s.recvfrom(65565))
    
    # disabled promiscuous mode
    #s.ioctl(socket.SIO_RCVALL, socket.RCVALL_OFF)
    input("get another?")
commented: thanks for the code +4

I think that's a bit advanced for me.. Thanks for your help guys!

It may be at the moment just work on the basics of the language first. Really get in depth on working with strings and lists, string and list slicing, functions, and classes. I'm still a big time noob, but things just start to make sence after you read enough and play around with it enough. Many refer to it as breaking the circle, and I have done it with many things, first string manipulations started making more sence, then using lists for certain operations started to make sence. I wouldn't move on to socket programming until you at least have a pretty solid base to work from, or you will just overshoot yourself. Although reading up on things never hurts either, one day you will wake up and it will all become much more clear.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.