Noob Question about Ping in Python

This is the place for queries that don't fit in any of the other categories.

Noob Question about Ping in Python

Postby eliabieri » Thu Oct 24, 2013 4:42 pm

Hello Guys.
I want to use Ping in Python. I want to ping an URL, and when the Server in unreachable(timeout) i want to execute a python script.
When the host is reachable again, i want to execute an other python script.
How do I do this the best?
Thank You
Last edited by micseydel on Thu Oct 24, 2013 5:57 pm, edited 1 time in total.
Reason: Locked OP.
eliabieri
 
Posts: 2
Joined: Thu Oct 24, 2013 4:40 pm

Re: Noob Question about Ping in Python

Postby Crimson King » Thu Oct 24, 2013 8:32 pm

Hi eliabieri, welcome to the forums. Make sure you read this thread New Users

You should post any attempts you made so far to reach your goal, no one is going to help you if they don't see you put some effort yourself.

Check out the subprocess module and see if you can come up with something.

Good luck.
User avatar
Crimson King
 
Posts: 85
Joined: Fri Mar 08, 2013 2:42 pm
Location: Buenos Aires, Argentina

Re: Noob Question about Ping in Python

Postby metulburr » Thu Oct 24, 2013 11:32 pm

python3.x
Code: Select all
import urllib.request

not_working_url = 'http://www.xyzdoesnotexist.com'
working_url = 'http://www.google.com'

try:
    print('{}: {}'.format(not_working_url, urllib.request.urlopen(not_working_url).getcode()))
except urllib.error.URLError:
    print('{}: server down or does not exist'.format(not_working_url))

try:
    print('{}: {}'.format(working_url, urllib.request.urlopen(working_url).getcode()))
except urllib.error.URLError:
    print('{}: server down or does not exist'.format(working_url))
\

python2.x
Code: Select all
import urllib2

not_working_url = 'http://www.xyzdoesnotexist.com'
working_url = 'http://www.google.com'


try:
    print('{}: {}'.format(not_working_url, urllib2.urlopen(not_working_url).getcode()))
except urllib2.URLError:
    print('{}: server down or does not exist'.format(not_working_url))

try:
    print('{}: {}'.format(working_url, urllib2.urlopen(working_url).getcode()))
except urllib2.URLError:
    print('{}: server down or does not exist'.format(working_url))


there are numerous methods on obtaining the same result. This is just the getcode/try and except way.
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1321
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Noob Question about Ping in Python

Postby eliabieri » Fri Oct 25, 2013 9:16 am

Thank you very much. And how do I do it when I want to ping a local IP Adress?
When the IP is reachable i want to run a file named "true.py" and when the IP is unreachable i want to run "false.py"
Thanks for your help!
eliabieri
 
Posts: 2
Joined: Thu Oct 24, 2013 4:40 pm

Re: Noob Question about Ping in Python

Postby metulburr » Fri Oct 25, 2013 10:12 am

replace the print statements with the execution of the scripts
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1321
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY


Return to General Coding Help

Who is online

Users browsing this forum: Bing [Bot], freddyhard, gutschy and 2 guests