首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为ping网站制作Python脚本

为ping网站制作Python脚本
EN

Stack Overflow用户
提问于 2018-02-07 19:08:26
回答 3查看 9.2K关注 0票数 0

所以我知道不是所有这些都是正确的,但这就是我所拥有的。我只是想写一个脚本,可以点击谷歌或雅虎,选择1或2和第三个选项,输入一个自定义的URL,如果有人可以帮助我解决这个Id感谢它。

代码语言:javascript
复制
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: ')
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2018-02-07 19:17:30

所以我把这个修好了,而且大部分都很管用:

代码语言:javascript
复制
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!")

希望这能帮上忙!

票数 1
EN

Stack Overflow用户

发布于 2018-09-26 03:41:29

下面是一个示例代码,它根据任何web地址(如: google.com)的ping状态提供消息。如果响应“确定”,则显示“活动”,否则将显示异常消息。

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

代码语言:javascript
复制
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)

票数 2
EN

Stack Overflow用户

发布于 2018-02-07 19:26:08

代码语言:javascript
复制
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")
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48671518

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档