ofe
Posts: 47
Joined: Wed Jun 05, 2013 4:05 am
Location: Italy,Milan
Contact: Website

How to ping a device in python?

Thu Mar 20, 2014 1:11 pm

Hello
I have my raspberry that controls (will control) a drone.
An additional devide (a pc , for instance) connected via wifi on teh same network, sends the commands to rpi.

I need to detect if raspberry loose connection to the device.

I can image to develop a "ping" funcion.
Can you suggest me some available library or giv eme some example?

It is mandatory that this chack has not to lock the main loop (eventually I can think to make a parallel thread).

BR
Oscar
Oscar

Visit my quadcopter blog
http://solenerotech1.wordpress.com

User avatar
paddyg
Posts: 2541
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: How to ping a device in python?

Thu Mar 20, 2014 9:44 pm

Not entirely what's pinging what but if you can run python on both then this would be quite easy. google things like 'python simple socket' or 'python simple server'
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

ofe
Posts: 47
Joined: Wed Jun 05, 2013 4:05 am
Location: Italy,Milan
Contact: Website

Re: How to ping a device in python?

Fri Mar 21, 2014 12:55 pm

Hi,
Image that in the rpi I have a webserver,
that I would like to connect from any device ( my pc, or a smartphone via browser).
So I would not limitate the connection check respect a specific socket connection.

O.
Oscar

Visit my quadcopter blog
http://solenerotech1.wordpress.com

User avatar
paddyg
Posts: 2541
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: How to ping a device in python?

Fri Mar 21, 2014 1:32 pm

Flask is a good place to start. I'm doing a project now for google devart and flask proved to very easy to set up and use.
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

Nander
Posts: 31
Joined: Tue Dec 04, 2012 11:21 pm
Location: The Hague - Netherlands

Re: How to ping a device in python?

Sat Mar 22, 2014 3:45 pm

The easiest way would be to use the python subprocess library and call ping itself and read the output. And if you need it run parallel you can use the threading library. Simple example, ping and return the value of packets received.

Code: Select all

import subprocess
subprocess.call("ping 8.8.8.8 -c 1 | grep 'received' | awk -F',' '{ print $2}' | awk '{ print $1}'", shell=True)
In this case it pings the google dns server once and prints out the response. Change the ip and packet nr to anything you want.

*Borrowed the code from cyberciti

aplocher
Posts: 16
Joined: Mon Aug 15, 2016 1:05 am

Re: How to ping a device in python?

Mon Aug 15, 2016 1:28 am

I realize this is an old thread, but just thought I would contribute a little.

@Nander: Doing string checks (the word "received" in your example) is always a concern for me. I worry that 'ping' might have different language variants that will cause it to break on other systems. Also when I run your code, I get back two lines:

Success:
1
0

or, Fail:
0
0

Plus, I believe Shell=True should be avoided (whenever possible). Here's an alternative that won't rely on a string result and will not require Shell=True:

Code: Select all

import subprocess
res = subprocess.call(["ping", "8.8.8.8", "-c1", "-W2", "-q"])
print(res)
"res" should be based on the exit code from ping. success: =0, fail: >0

Something like this should suppress the output, too:

Code: Select all

res = subprocess.call(["ping", "8.8.8.8", "-c1", "-W2", "-q"], stdout=open(os.devnull,'w'))

Errorplayer
Posts: 10
Joined: Wed Sep 27, 2017 10:49 am

Re: How to ping a device in python?

Sat Dec 02, 2017 1:30 pm

Hello
just to add:

I tried both codes sugested above.
this one

import subprocess
subprocess.call("ping 8.8.8.8 -c 1 | grep 'received' | awk -F',' '{ print $2}' | awk '{ print $1}'", shell=True)

just gives me as an output 127. Even when i am diconnected.
and the other one:

import subprocess
res = subprocess.call(["ping", "8.8.8.8", "-c1", "-W2", "-q"])
print(res)

did work in Shell but not in Thonny.
In thonny i did not get an output.
With

import subprocess
res = subprocess.call(["ping", "8.8.8.8", "-c1", "-W2", "-q"], stdout=open(devnull, 'w'))
print(res)

it worked perfectl in Thonny.

So use the second code.

Return to “Python”