[python-win32] Getting Network Information/Statistics

Tim Roberts timr at probo.com
Wed Jan 23 19:11:14 CET 2008


 >.> wrote:
> I got the interface name from win32util.browse() in Vista and XP. I 
> think XP is Giving me the wrong number because it differs from what 
> I'm reading in perfmon everytime and it seems to be counting down from 
> that number after every subsequent call.
> ex: perfmon will give me a last of 0.0 and
>  win32pdhutil.GetPerformanceAttributes('Network Interface','Bytes 
> Received/sec','Intel[R] PRO_100 VE Network Connection - Packet 
> Scheduler Miniport')
>
> Will return something like 12107 and will go down every subsequent call.

Insert a call to win32pdh.CollectQueryData(hq) before you fetch the 
counter value.  The counters are only fetched when you send a query, and 
that's done with CollectQueryData.

For me, this produces exactly the same numbers as perfmon:

import win32pdh
import time
intf = "NVIDIA nForce Networking Controller - Packet Scheduler Miniport"
hq = win32pdh.OpenQuery()
cp = win32pdh.MakeCounterPath( (None, 'Network Interface', intf, None, 
-1, 'Bytes Received/sec') )
hc = win32pdh.AddCounter( hq, cp )
for i in range(100):
    win32pdh.CollectQueryData( hq )
    tp,val = win32pdh.GetFormattedCounterValue( hc, win32pdh.PDH_FMT_LONG )
    print hex(tp),val
    time.sleep(1)

-- 
Tim Roberts, timr at probo.com
Providenza & Boekelheide, Inc.



More information about the python-win32 mailing list