制作Python脚本来ping网站

时间:2018-02-07 19:08:26

标签: python

所以我知道并非所有这些都是正确的,但这就是我所拥有的。我只是想编写一个可以ping谷歌或雅虎的脚本,通过选择1或2和第三个选项来输入自定义URL,如果有人可以帮助我,我会欣赏它。

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: ')

3 个答案:

答案 0 :(得分:0)

所以我对此进行了修改,并且大部分都有效:

Option Explicit


Public Sub ReadLargeFile(ByVal fileCurrent As Scripting.File)

    Dim FileStream As ADODB.Stream
    Set FileStream = New ADODB.Stream
    FileStream.Type = adTypeText
    FileStream.LineSeparator = adCRLF
    FileStream.Mode = adModeReadWrite
    FileStream.Open
    FileStream.LoadFromFile fileCurrent.Path
    MsgBox FileStream.ReadText(-2)
    FileStream.Close

End Sub

希望这有帮助!

答案 1 :(得分: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")

答案 2 :(得分:0)

以下是示例代码,该代码根据ping状态为任何网址(例如:google.com)提供消息。如果响应为“ ok”,则显示“ ALIVE”,否则将显示异常消息。

使用python的Raspberry-pi的示例平台:

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