473,387 Members | 1,789 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Ping monitor - monitor ip in the =?UTF-8?Q?background=3F?=


With python 2.6 and wxpython I'm trying to create a system tray icon
application based around an example found here:
http://codeboje.de/MailSneaker-Part-...-and-wxPython/

The application will simply change the systray icon based on if an ip
address is online or not.

The ping portion looks like this:

if os.name == "nt": # Windows
pcmd = "ping -n 1 -w 1000 "
else: # *nix
pcmd = "ping -c1 -W1 "

def Ping(ipaddress):
# execute the code and pipe the result to a string
p = subprocess.Popen(pcmd + ipaddress, shell=True,
stdout=subprocess.PIPE)
# give it time to respond
p.wait()

a = re.search('(.*)ms', p.stdout.read())
if a:
return True
else:
return False

I've been able to add the ping check as a manual process (via a right
click menu item) but very confused on what direction to take on making
the ping a permanent loop while the app is running.

I was looking at making the ping routine a thread process but can't
figure out how to feed back the result to the calling app. Global
variable??

Ideas?

Nov 2 '08 #1
1 72521
Hi, ScottZ.

I I have to write such a thing, I'll wrap the whole thing into some
class, say Pinger. It will have "do" method, which will perform one
particular pinging action. It'll also have a start/stop mechanism,
which will start a thread to continuously pinging a host. To notify
environment (say, yours tray icon) about host state change (dead/
alive), it will have callback mechanism (register_callback/
unregister_callback). Here, I've written a simple implementation,
maybe this will be helpful.

====== pinger.py ================
import os
import threading
import subprocess
import re
import time

class Pinger:
def __init__(self, ip = None):
self.ip = None
self.running = False
self.callbacks = list()
self.setAddress(ip)
def setAddress(self, ip):
if self.ip != ip:
if self.running:
self.stop()
self.ip = ip
def do(self):
if os.name == "nt": # Windows
pcmd = "ping -n 1 -w 1000 "
else: # *nix
pcmd = "ping -c1 -W1 "
p = subprocess.Popen(pcmd + self.ip, shell=True,
stdout=subprocess.PIPE)
# give it time to respond
p.wait()
a = re.search('(.*)ms', p.stdout.read())
if a:
return True
else:
return False
def start(self):
def run():
result = False
while self.running:
next = self.do()
if next != result and self.running:
[ callback(next) for callback in self.callbacks ]
result = next
self.ping_thread = threading.Thread(target = run)
self.running = True
self.ping_thread.start()
def stop(self):
self.running = False
def register_callback(self, callback):
if callback not in self.callbacks:
self.callbacks.append(callback)
def unregister_callback(self, callback):
if callback in self.callbacks:
self.callbacks.remove(callback)

if __name__ == '__main__':
p = Pinger('192.168.1.1')
def printout(alive):
if alive:
print 'Host is alive.'
else:
print 'Host is dead.'
p.register_callback(printout)
p.start()
while True:
print "Ding..."
time.sleep(1)

================

Note that printout will be called ONLY if host state has changed, not
on EVERY ping.

--
Best Regards, Andrey Balaguta
Nov 2 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Marty McDonald | last post by:
If I invoke a web service, and it throws an exception, I can see the exception if the client app is a .Net app. However, if the client app is not a .Net app, I only receive the HTTP 500 error. I...
6
by: jmgonet | last post by:
Hello everybody, I'm having troubles loading a Xml string encoded in UTF-8. If I try this code: ------------------------------ XmlDocument doc=new XmlDocument(); String s="<?xml...
1
by: rmgalante | last post by:
I have a Windows Service that reads and writes an XML file to disk periodically. I use the XmlSerializer to serialize and deserialize the XML file on disk. I am writing the file using an...
2
by: RodBillett | last post by:
I have an ASP.NET web page that accepts querystring parameters. MyPage.aspx?DB=__??????????&ID=1 When I get into the page and look at Request.Querystring it shows as ...
1
by: romiko2000 | last post by:
Hi Folks, I got a weird problem, I create an XMLWriter to post a document via the webrequest stream and after running a network trace, I notice the data is prefixed with 3 invalid characters! ...
9
by: gene.james | last post by:
One of those cases where the problem is probably right under my nose but I can't see it. This serial test code fails to send the right data: Private Sub Button3_Click(ByVal sender As...
4
by: rgparkins | last post by:
Hello I am running out of time with a problem I have running PHP 5.04 and Apache 2.0 and really need help :(. I have a page that stores a variable in session but each time I reload that page the...
94
by: Samuel R. Neff | last post by:
When is it appropriate to use "volatile" keyword? The docs simply state: " The volatile modifier is usually used for a field that is accessed by multiple threads without using the lock...
3
by: querry | last post by:
Hi all I have a XML file and I am trying to transform and create another XML file using the XSLT. I need to check a particular node belongs to a specific namespace or not. How do I do it in the .xsl...
1
by: bharath652 | last post by:
Hi, I need to know how to get the Mime type of a Document from the Byte array. I'm not having file name of file extension. I have only Byte array of a document. To be more specific, Using the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.