/usr/share/pyshared/pychess/System/ping.py is in pychess 0.12~beta3-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 | # -*- coding: UTF-8 -*-
from gobject import GObject, SIGNAL_RUN_FIRST
from pychess.System.Log import log
from pychess.System.SubProcess import SubProcess, searchPath
import re
class Pinger (GObject):
""" The recieved signal contains the time it took to get response from the
server in millisecconds. -1 means that some error occurred """
__gsignals__ = {
"recieved": (SIGNAL_RUN_FIRST, None, (float,)),
"error": (SIGNAL_RUN_FIRST, None, (str,))
}
def __init__ (self, host):
GObject.__init__(self)
self.host = host
self.subproc = None
self.expression = re.compile("time=([\d\.]+) (m?s)")
# We need untranslated error messages in regexp search
# below, so have to use deferred translation here
def _(msg): return msg
error = _("Destination Host Unreachable")
self.errorExprs = (
re.compile("(%s)" % error),
)
del _
self.restartsOnDead = 3
self.deadCount = 0
def start (self):
assert not self.subproc
self.subproc = SubProcess(searchPath("ping"), [self.host], env={"LANG":"en"})
self.conid1 = self.subproc.connect("line", self.__handleLines)
self.conid2 = self.subproc.connect("died", self.__handleDead)
def __handleLines (self, subprocess, lines):
for line in lines:
self.__handleLine(line)
def __handleLine (self, line):
match = self.expression.search(line)
if match:
time, unit = match.groups()
time = float(time)
if unit == "s":
time *= 1000
self.emit("recieved", time)
else:
for expr in self.errorExprs:
match = expr.search(line)
if match:
msg = match.groups()[0]
self.emit("error", _(msg))
def __handleDead (self, subprocess):
if self.deadCount < self.restartsOnDead:
log.warn("Pinger died and restarted (%d/%d)\n" %
(self.deadCount+1, self.restartsOnDead),
self.subproc.defname)
self.stop()
self.start()
self.deadCount += 1
else:
self.emit("error", _("Died"))
self.stop()
def stop (self):
assert self.subproc
exitCode = self.subproc.gentleKill()
self.subproc.disconnect(self.conid1)
self.subproc.disconnect(self.conid2)
self.subproc = None
if __name__ == "__main__":
pinger = Pinger("google.com")
def callback(pinger, time):
print time
pinger.connect("recieved", callback)
pinger.start()
import time
time.sleep(5)
pinger.stop()
time.sleep(3)
|