Measuring bytes of packet sent from python application

alex goretoy aleksandr.goretoy at gmail.com
Mon Jan 5 22:00:01 EST 2009


You can alot run "lsof -i -T -n" from popen2 to see what applications use
what port and addr they are connected to.
In order to see all jobs though it needs to be ran as root. This way you can
find out what ports and such Then perform tcp dump thru popen2 and filter
for port and addr. This is just a first shot, then I would figure out what
to do after having gotten to this point.

import popen2
#find out what ports and addrs what program is connected to
r,w,e=popen2.popen3("lsof -i -T -n") # this may need sudo prepended to lsof
r=r.readlines()
e=e.readlines()
#get tcpdump, doesn't quiet work here....needs something more, maybe a
different way altogether.
#tcpdump to pcap file and read file periodically?
r1,w1,e1=popen2.popen3("sudo tcpdump -i eth1")
r1=r1.readlines()
e1=e1.readlines()

Something to this nature, then do regular expression on tcpdump output,
either logged to pcap output file or by other means.

-Alex Goretoy
http://www.alexgoretoy.com
somebodywhocarez at gmail.com


On Mon, Jan 5, 2009 at 8:14 PM, Grant Edwards <grante at visi.com> wrote:

> On 2009-01-05, Kangkook Jee <aixer77 at gmail.com> wrote:
>
> > I'd like to measure number of bytes sent(or recv'd) from my
> > python application. Does anyone have any idea how can I
> > achieve this?
>
> Aside from tcpdump/wireshark, you can set up iptables rules to
> enable accounting for traffic based on IP addresses and ports.
>
> http://wiki.openvz.org/Traffic_accounting_with_iptables
> http://www.faqs.org/docs/linux_network/x-087-2-accounting.ipfwadm.html
> http://www.catonmat.net/blog/traffic-accounting-with-iptables/
>
> --
> Grant
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20090105/3e32b68c/attachment-0001.html>


More information about the Python-list mailing list