[BangPypers] Background ping - check for server availability

kracekumar ramaraju kracethekingmaker at gmail.com
Thu Dec 29 19:44:54 CET 2011


Hi

Inline replies.

On Thu, Dec 29, 2011 at 5:23 PM, <Nikunj.Badjatya at emc.com> wrote:

> Hi,
> I want to check the availability of the server ( in the sense ) by
> continuously pinging it in intervals of 30 seconds.
> I am using Python2.7 on windows platform.
> I wrote the following snippet. It works fine but it opens a windows cmd
> prompt everytime it is reaching subprocess.call()
>
> {{{
> import subprocess
> import time
>
> def checkserveravail(ip):
>    while True:
>        pingret = subprocess.call('ping {0} -n 1'.format(esxname))
>        if pingret == 0:
>            time.sleep(30)
>            continue
>        else:
>            print("The  server {0} is not reacheable. Please check network
> connectivity".format(ip))
>
>
> checkserveravail('xx.xx.xx.xx')
>
> }}}
>
> This snippet will be a part of larger program. Where a thread will be
> doing this job of checking server recheability.
>
> I do not want that cmd prompt windows to open everytime and just want this
> job to happen in background.
> I also do not want to import any external module like ping, which will
> unnecessarily increase the dependencies in my programs.
>
> Any clues?
>
>
You can try this, I have tried this GNU/Linux.

In[8]: import commands

In [9]: commands.getstatusoutput("ping -c 1 www.google.com")
Out[9]:
(0,
 'PING www.l.google.com (74.125.236.146) 56(84) bytes of data.\n64 bytes
from bom03s02-in-f18.1e100.net (74.125.236.146): icmp_req=1 ttl=56 time=737
ms\n\n--- www.l.google.com ping statistics ---\n1 packets transmitted, 1
received, 0% packet loss, time 0ms\nrtt min/avg/max/mdev =
737.818/737.818/737.818/0.000 ms')

replace google.com with IP.

In case your intension is to check whether website hosted in the IP is
active, you can use requests. requests needs to be installed in your
machine.

www.python-requests.org

In [10]: import requests

In [11]: r = requests.get("http://www.google.com")

In [12]: r.status_code
Out[12]: 200

Note: If "http" is ignoree in requests.get,  an error is raised.


Thanks,
> Nikunj
>
> 7  Switch off as you go  | qRecycle always  | P  Save Paper - Save Trees |
> Go Green
>
> _______________________________________________
> BangPypers mailing list
> BangPypers at python.org
> http://mail.python.org/mailman/listinfo/bangpypers
>



-- 
*
Thanks & Regards

"Talk is cheap, show me the code" -- Linus Torvalds
kracekumar
www.kracekumar.com
*


More information about the BangPypers mailing list