Hi, all
I am studying Python recently. I have written a example code to create a GTK window.
Below is the sample code:
import pygtk
pygtk.require('2.0')
import gtk
import gtk.glade
import os
import sys
import re
from signal import SIGKILL
from math import floor
from time import time
import trace
import threading
from twisted.internet import reactor
from twisted.internet.utils import getProcessOutput
from twisted.python import log
from twisted.application.internet import TimerService
class Bar:
def __init__(self):
self.downmsg = None
self.upmsg = None
self.speed = {'down': 0.0, 'up': 0.0}
self._time = 0
self._inbits = 0
self._outbits = 0
self.wTree = gtk.glade.XML("main.glade", "window1")
self.window = self.wTree.get_widget("window1")
self.window.show_all()
def speed(self):
CMD = 'cat'
SPEED_PATH = '/proc/net/dev'
regexp2 = re.compile(r"""
\s+%(wface)s:\s+
(?P<in>\d+)
\s+\d+\s+\d+\s+\d+\s+\d+\s+\d+\s+\d+\s+\d+\s+
(?P<out>\d+)
""" % dict(wface='eth1'), re.VERBOSE)
def speed_parse_input(text2):
"""Extracts the recv and sent bytes from /proc/net/dev"""
inbits = None
outbits = None
match2 = regexp2.search(text2)
if match2:
# /proc/net/dev counts bytes not bits
inbits = int(match2.group('in')) * 8
outbits = int(match2.group('out')) * 8
now = time()
interval = now - self._time
in_diff = inbits - self._inbits
out_diff = outbits - self._outbits
self.speed = int(floor(in_diff / interval))
self.speed = int(floor(out_diff / interval))
self._inbits = inbits
self._outbits = outbits
self._time = now
if self.speed > 1000:
upspeed = self.speed / 1000.0
downspeed = self.speed / 1000.0
self.upmsg = (upspeed > 1000) and "%3.2f Mbps (WiFi)" % (upspeed / 1000) or \
"%3.2f Kbps (WiFi)" % upspeed
self.downmsg = (downspeed > 1000) and "%3.2f Mbps (WiFi)" % (downspeed / 1000) \
or "%3.2f Kbps (WiFi)" % downspeed
else:
self.upmsg = "%3.2f bps (WiFi)" % self.speed
self.downmsg = "%3.2f bps (WiFi)" % self.speed
d1 = getProcessOutput(CMD, args=[SPEED_PATH])
d1.addCallback(speed_parse_input)
if self.upmsg != None:
self.statusbar6 = self.wTree.get_widget("statusbar6")
self.statusbar6.push(1, self.upmsg)
if self.downmsg != None:
self.statusbar7 = self.wTree.get_widget("statusbar7")
self.statusbar7.push(1, self.downmsg)
if __name__ == '__main__':
a = Bar()
log.msg("hello, world")
a.speed()
gtk.main()
My problem is that the log.msg()
and the getProcessOutput()
functions seem not to work.
I could not find any useful answer on google.
Please help me. Thank you!