Indicator for Network Status
来源:互联网 发布:淘宝平台费用是多少 编辑:程序博客网 时间:2024/06/06 21:03
GNOME可以用的最小的网络指示器
#!/usr/bin/python# -*- coding: utf-8 -*-"""Created on Tue Nov 11 21:26:17 2014@author: terry"""import pygtkpygtk.require('2.0')import sysimport osimport shutilimport jsonimport timefrom threading import Thread, Eventimport subprocessimport psutil as psimport copyimport reimport gtkgtk.gdk.threads_init()import appindicatorimport loggingclass StatusUpdateIndicator(Thread): B_UNITS = ['', 'KB', 'MB', 'GB', 'TB'] interval = 2 def __init__(self): Thread.__init__(self) self.isRunning = True self.last = ps.cpu_times() self.last_net_usage = [0, 0] # (up, down) self.network_indicator = appindicator.Indicator ("indicator-sysmonitor", "sysmonitor", appindicator.CATEGORY_APPLICATION_STATUS) self.network_indicator.set_status (appindicator.STATUS_ACTIVE) self.network_indicator.set_icon("gnome-netstatus-idle") self.network_indicator.set_label("Init...") # create a menu self.menu = gtk.Menu() image = gtk.ImageMenuItem(gtk.STOCK_QUIT) image.connect("activate", self.quit) image.show() self.menu.append(image) self.menu.show() self.network_indicator.set_menu(self.menu) def bytes_to_human(self,bytes_): unit = 0 while bytes_ > 1024: unit += 1 bytes_ /= 1024 return '{}{}'.format(int(bytes_), self.B_UNITS[unit]) def update(self): """It returns the bytes sent and recieved in bytes/second""" current = [0, 0] for _, iostat in ps.network_io_counters(pernic=True).items(): current[0] += iostat.bytes_recv current[1] += iostat.bytes_sent dummy = copy.deepcopy(current) current[0] -= self.last_net_usage[0] current[1] -= self.last_net_usage[1] self.last_net_usage = dummy current[0] /= self.interval current[1] /= self.interval if current[0] == 0 and current[1] == 0: self.network_indicator.set_icon("gnome-netstatus-idle") elif current[0] > 0 and current[1] == 0: self.network_indicator.set_icon("gnome-netstatus-rx") elif current[0] == 0 and current[1] > 0: self.network_indicator.set_icon("gnome-netstatus-tx") else: self.network_indicator.set_icon("gnome-netstatus-txrx") traffic = '{}/s | {}/s'.format(self.bytes_to_human(current[1]), self.bytes_to_human(current[0]))# print traffic self.network_indicator.set_label(traffic) def run(self): """It is the main loop.""" while self.isRunning: self.update() time.sleep(self.interval) def quit(self, widget, data=None): self.isRunning = False gtk.main_quit()def main(): gtk.main() return 0if __name__ == "__main__": indicator = StatusUpdateIndicator() indicator.setDaemon(True) indicator.start() main()#network_indicator = appindicator.Indicator("indicator-sysmonitor",# "sysmonitor",# appindicator.CATEGORY_SYSTEM_SERVICES)#network_indicator.set_status(appindicator.STATUS_ACTIVE)#network_indicator.set_label("Init...")#network_indicator.set_icon("gnome-netstatus-idle")##updater = StatusUpdater(network_indicator)
0 0
- Indicator for Network Status
- Virtualization for Network-based Multicore Telecommunication Systems – Current Status
- Indicator
- Indicator for invoice receipt used not allowed
- CapsLock indicator on Ubuntu for Thinkpad
- android: check network status 检查网络状态
- TelephonyManager-get SIM card and network status
- HTTP Status Codes for Beginners
- Status bar colour for UIImagePickerController
- Resources for Social Network
- dos command for network
- some link for network
- waiting for network configuration
- mac commands for network
- Network Programming for Microsoft Windows
- Oracle Ports for Network Services
- ubuntu修改interfaces for network
- Configuring Default-network for EIGRP
- C# DateTime 转文本,例如:1年前,3个月前,3周前,6天前,15小时前,35分钟前
- GlusterFS数据恢复机制AFR
- 贝叶斯网络
- 哈夫曼树,及哈夫曼编码的构造
- hdu3401_分析降维_队列优化
- Indicator for Network Status
- 子节点的位置与父节点位置的关系
- JQuery处理json 与 ajax返回JSON实例
- CoAP协议及开源实现
- JBOSS安装配置详细教程
- memcache学习笔记
- php使用socket获取远程图片
- Java Socket编程
- ActionBar和DrawerLayout纯原生控件,实现侧滑栏和滑动Tab界面