Reputation: 53
So I know not all of this is correct but this is what I have. I'm just trying to write a script that can ping google or yahoo by selecting 1 or 2 and a third option to enter a custom URL if anyone could help me out with that Id appreciate it.
import os
print('1. Ping Google')
print('2. Ping Yahoo')
print('3. Ping custom URL')
key = input('Input your choice: ')
if key == 1:
os.system("ping google.com")
elif key == 2:
os.system("ping yahoo.com")
elif key == 3:
input('Enter URL: ')
Upvotes: 0
Views: 10612
Reputation: 937
Here is an example code which provide message according to ping status for any web address (like: google.com) . If response "ok" then show "ALIVE" otherwise will show exception message.
Example platform for Raspberry-pi using python:
import requests
import time
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.OUT)
while True:
try:
response = requests.get('http://google.com')
print(int(response.status_code))
if response.status_code == requests.codes.ok:
GPIO.output(17, GPIO.LOW)
print "ALIVE"
else:
GPIO.output(17, GPIO.HIGH)
print "DISCONNECTED"
time.sleep(1)
except Exception as e:
print "NO INTERNET"
GPIO.output(17, GPIO.HIGH)
print str(e)
Upvotes: 2
Reputation: 367
import os
print('1. Ping Google')
print('2. Ping Yahoo')
print('3. Ping custom URL')
key = input('Input your choice: ')
if key == 1:
os.system("ping google.com")
elif key == 2:
os.system("ping yahoo.com")
elif key == 3:
url=raw_input('Enter URL: ')
os.system("ping %s" %url)
else:
print("invalid input")
Upvotes: 0
Reputation: 81
So I got this fixed up a bit and mostly works:
from os import system
print('1. Ping Google')
print('2. Ping Yahoo')
print('3. Ping custom URL')
key = int(input('Input your choice: '))
if key == 1:
system("ping www.google.com")
elif key == 2:
system("ping www.yahoo.com")
elif key == 3:
url = input('Enter URL: ')
system("ping " + url)
else:
print("Invalid Option!")
Hope this helped!
Upvotes: 1