usr bin env python coding utf-8 import re sys os from time import slee

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import re, sys, os
from time import sleep
_widgetName = "network"
_awesome_client_path = "%s/bin/awesome-client" % os.environ.get('HOME')
#_ifaces = ['eth0', 'eth1', 'ppp0']
_ifaces = [ 'eth0' ]
ifaces = arp = ''
def get_ifaces():
global ifaces, arp
f = open('/proc/net/dev', 'r')
ifaces = f.read()
f.close()
ff = open('/proc/net/arp', 'r')
arp = ff.read()
ff.close()
def bytes_to_human(bytes):
if len(bytes)<3:
return '%sb' % bytes
elif len(bytes)<6:
#return '%5.2fKb' % round(float(bytes)/1024.0, 1)
return '%sKb' % str(round(float(bytes)/1024.0, 1))
else:
#return '%5.2fMb' % round(float(bytes)/1048576.0, 1)
return '%sMb' % round(float(bytes)/1048576.0, 1)
def get_net_status():
global ifaces
stat = ''
for i in _ifaces:
if stat != '':
stat += ' | '
if (arp.find(i) != -1) and (ifaces.find(i) != -1):
dspeed, uspeed = re.search(r'.*%s:\s*([0-9]*)\s*[0-9]*\s*[0-9]*\s*[0-9]*\s*[0-9]*\s*[0-9]*\s*[0-9]*\s*[0-9]*\s*([0-9]*).*' % i , ifaces).groups()
dspeed, uspeed = bytes_to_human(dspeed), bytes_to_human(uspeed)
stat += '%s: %s/%s' % (i, dspeed, uspeed)
else:
stat += '%s: off' % i
return '[ %s ]' % stat
if __name__ == "__main__":
if len(sys.argv) > 1:
_widgetName = sys.argv[1]
while 1:
try:
get_ifaces()
pipe = os.popen(_awesome_client_path, 'w')
pipe.write( "0 widget_tell %s %s\n" % (_widgetName, get_net_status()) )
pipe.close()
#print get_net_status()
finally:
pass
sleep(1)