how to measure TCP congestion windows using python ??

Stefan Sonnenberg-Carstens stefan.sonnenberg at pythonmeister.com
Sun Dec 19 08:56:27 EST 2010


Am 19.12.2010 11:39, schrieb plz:
> hi! i'm newbie about python and i want to measure the value of cwnd in TCP socket.
>
> I have searched from an internet and got that we could handle it from SOL_TCP, TCP_INFO...since unix had #include<netinet/tcp.h>  and we could  refer to tcp_info
> for getting the information of TCP by using this method
>
> but i don't know how to declare the tcp_info in Python..
> does anyone know how to handle this? please suggest me
>
> Thanks in advance
>
>
Here is non-portable way to get the data under Linux:

import socket
from ctypes import *
cdll.LoadLibrary('libc.so.6')
libc = CDLL('libc.so.6')

class tcp_info(Structure):
     _fields_  = [
	('tcpi_state',c_uint8),
	('tcpi_ca_state',c_uint8),
	('tcpi_retransmits',c_uint8),
	('tcpi_probes',c_uint8),
	('tcpi_backoff',c_uint8),
	('tcpi_options',c_uint8),
	('tcpi_snd_wscale',c_uint8,4),
     ('tcpi_rcv_wscale',c_uint8,4),
	('tcpi_rto',c_uint32),
	('tcpi_ato',c_uint32),
	('tcpi_snd_mss',c_uint32),
	('tcpi_rcv_mss',c_uint32),
	('tcpi_unacked',c_uint32),
	('tcpi_sacked',c_uint32),
	('tcpi_lost',c_uint32),
	('tcpi_retrans',c_uint32),
	('tcpi_fackets',c_uint32),
	('tcpi_last_data_sent',c_uint32),
	('tcpi_last_ack_sent',c_uint32),
	('tcpi_last_data_recv',c_uint32),
	('tcpi_last_ack_recv',c_uint32),
	('tcpi_pmtu',c_uint32),
	('tcpi_rcv_ssthresh',c_uint32),
	('tcpi_rtt',c_uint32),
	('tcpi_rttvar',c_uint32),
	('tcpi_snd_ssthresh',c_uint32),
	('tcpi_snd_cwnd',c_uint32),
	('tcpi_advmss',c_uint32),
	('tcpi_reordering',c_uint32),
	('tcpi_rcv_rtt',c_uint32),
	('tcpi_rcv_space',c_uint32),
	('tcpi_total_retrans',c_uint32)
     ]

sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM,socket.IPPROTO_TCP)
info = tcp_info()
pinfo = pointer(info)
len_info = c_uint32(sizeof(tcp_info))
plen_info = pointer(len_info)
res = libc.getsockopt(sock.fileno(),socket.SOL_TCP,socket.TCP_INFO,pinfo,plen_info)
if res == 0:
     for n in (x[0] for x in tcp_info._fields_):
         print n,getattr(info,n)
else:
     print 'getsockopt() failed. (%i)' % res

Tested under Ubuntu 10.10 and Python 2.6.6.




More information about the Python-list mailing list