#!/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)