[BangPypers] Background ping - check for server availability

Nikunj Badjatya nikunjbadjatya at gmail.com
Thu Dec 29 20:32:01 CET 2011


Hi,

I think the commands.getstatusoutput() is only for unix systems.
When I run on windows I get this error:

{{{
>>> ret = commands.getstatusoutput('ping 192.168.1.1')
>>> ret
(1, "'{' is not recognized as an internal or external command,\noperable
program or batch file.")
}}}

I then tried once again with subprocess module:

{{{
>>> pipe = subprocess.Popen('ping 192.168.1.1', shell=True,
universal_newlines=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
>>> sts = pipe.wait()
>>> sts
0
}}}

and this seems to work exactly as I wanted and it does not open a windows
cmd prompt window.
Previously I was using 'shell=False' . After little more reading, I
understood the difference and got the correct way.

Thanks,

Nikunj


--
*7*Switch off as you go |*q*Recycle always | P Save Paper - Save Trees | Go
Green



On Fri, Dec 30, 2011 at 12:14 AM, kracekumar ramaraju <
kracethekingmaker at gmail.com> wrote:

> 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
> *
> _______________________________________________
> BangPypers mailing list
> BangPypers at python.org
> http://mail.python.org/mailman/listinfo/bangpypers
>


More information about the BangPypers mailing list