473,698 Members | 2,411 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Pope n(pcmd + ipaddress, shell=True,
stdout=subproce ss.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 72544
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_callb ack/
unregister_call back). 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.Pope n(pcmd + self.ip, shell=True,
stdout=subproce ss.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_threa d = threading.Threa d(target = run)
self.running = True
self.ping_threa d.start()
def stop(self):
self.running = False
def register_callba ck(self, callback):
if callback not in self.callbacks:
self.callbacks. append(callback )
def unregister_call back(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_call back(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
4195
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 believe this is due mostly to the fact that the non-.Net apps are using POST instead of SOAP. If the non-.Net apps were using SOAP, they too would in fact "see" the exception. Is this true? Thanks!
6
18755
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 version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\"?><a>Schönbühl</a>"; doc.LoadXml(s); doc.Save("d:\\temp\\test.xml");
1
5756
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 XmlTextWriter and UTF8 encoding. I notice after long periods of time that my file has bad, non-displayable characters. I try to load the XML file with IE, and it tells me that the XML is bad for one of many different reasons, depending on where the bad...
2
2542
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 "DB=__%3f%3f%3f%3f%3f%3f%3f%3f%3f%3f&ID=1"
1
2977
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! The invalid characters are: ef bb bf, at the top of the stream, which are . Why is this occuring, I am totally baffled by this? Here is the stream:
9
7516
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 System.Object, ByVal e As System.EventArgs) Handles ButtonCAM1.Click Dim TestData() As Byte = {&HFE, &H11, &H11, &HFF} Using KeyerCom As IO.Ports.SerialPort = _ My.Computer.Ports.OpenSerialPort(KeyerPortName, 38400, IO.Ports.Parity.None, 8,...
4
2871
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 session seems to be re-created and is an empty array. I have checked the session file and the variable is being stored against the session id, but I dont know why PHP is not picking up the session after I reload.. I have tried the usual suspects...
94
30309
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 Statement (C# Reference) statement to serialize access. " But when is it better to use "volatile" instead of "lock" ?
3
1829
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 file?? many thanks.
1
7719
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 binary of a docuement, I need to find whether it is MS Word or MS Excel and so on. Please tell me is there a way to find it in Java. Thanks in advance
0
8604
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9160
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9029
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8897
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7729
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6521
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4370
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3050
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2002
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.