0

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: ')
2
  • What trouble are you having? you seem to already know how to read user input. Commented Feb 7, 2018 at 19:12
  • 1
    code seems work just fine. what part do you need help with? for option 3., complete it with url = raw_input('Enter URL: '); os.system("ping " + url) Commented Feb 7, 2018 at 19:12

3 Answers 3

2

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)

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

1

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!

1 Comment

Thank you so much! I wasn't expecting a reply so quickly
0
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")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.