python判断输入是不是整数
作者:王忘杰
全民制作人大家好,我是学习python两天半的练习生王忘杰,喜欢路由交换、linux、网络安全,开整!这是我的第二篇0基础python文章,请大家支持,谢谢~
开发思路
用于监控指定的URL,在我的场景中,我是从腾讯VPS上监控公司宽带出口,当URL访问超时时,即为宽带故障。
设计思路很简单,访问失败发送告警,访问成功发送恢复通知,同时要使用配置文件进行配置,防止持续重复告警。
绘制开发流程图
使用python语言实现
#!/usr/bin/python3import requests
import time
import json
import os# 监测URL是否正常响应def url_check(url): # 当前时间 check_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
try: # 请求URL, 设置3s超时, 忽略SSL证书 r = requests.get(url, timeout=3, verify=False)
if r.status_code == 200:
fo = open(config,"r")
line = fo.read(1)
print(line) #判断配置文件内容 if line == "1":
print("发送报警")
fo.close()
fo = open(config, "w")
fo.write('0')
print("配置重置为0") # 请求响应状态 msg = "监控的URL:%s%sURL恢复状态正常:%s%s监测时间:%s" % (
url, "nn", r.status_code, "nn", check_time) # 推送消息 yun_push(msg)
else:
print("当前配置为",line)
fo = open(config, "w")
fo.write('0')
print("配置重置为0")
else:
fo = open(config, "r")
line = fo.read(1)
print(line)
if line == "1":
print("退出程序")
else:
print("发送报警")
fo.close()
fo = open(config, "w")
fo.write('1')
print("配置重置为1") # 请求响应状态 msg = "监控的URL:%s%sURL访问异常:%s%s监测时间:%s" % (
url, "nn", r.status_code, "nn", check_time) # 推送消息 yun_push(msg)
except:
fo = open(config, "r")
line = fo.read(1)
print(line)
if line == "1":
print("退出程序")
else:
print("发送报警")
fo.close()
fo = open(config, "w")
fo.write('1')
print("配置重置为1") # 请求响应状态 msg = "监控的URL:%s%sURL访问失败,无法连接%s监测时间:%s" % (
url, "nn", "nn", check_time) # 推送消息 yun_push(msg)
def yun_push(content):
url = 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=用自己的'
s = json.dumps({'msgtype': 'text', 'text': {"content" : content}})
print(requests.post(url, data=s).text)
if __name__ == '__main__': #判断配置文件是否存在,不存在则生成配置文件并退出,配置文件则存在执行 config = './baidu.config'
if not os.path.exists(config):
print("配置文件不存在")
file = open(config,'w')
file.close()
print("配置文件已生成")
else:
print("配置文件存在,执行URL检测")
url_check("https://www.baidu.com/")
运行效果
正式使用
在VPS部署python脚本,并设置定时任务即可
查看文章精彩评论,请前往什么值得买进行阅读互动
","gnid":"96c6a833d821e0468","img_data":[{"flag":2,"img":[{"desc":"","height":"274","title":"","url":"https://p0.ssl.img.360kuai.com/t015394727360c3cdc6.jpg","width":"600"},{"desc":"","height":"616","title":"","url":"https://p0.ssl.img.360kuai.com/t016ff92dc7f2116c3c.jpg","width":"359"}]}],"original":0,"pat":"art_src_1,fts0,sts0","powerby":"hbase","pub_time":1683810195000,"pure":"","rawurl":"http://zm.news.so.com/41b69145e2771cb08cd67bac9f05abc6","redirect":0,"rptid":"4cdf902d8b0d23e5","rss_ext":[],"s":"t","src":"什么值得买","tag":[{"clk":"ktechnology_1:机器人","k":"机器人","u":""}],"title":"0基础上手python编程,实践URL监控+企业微信机器人告警
江刚慧4638在python中,如何利用异常处理机制判断用户输入的合规性? -
冉皇褚19729984412 ______ try的时候,通过if/else判断输入是否合规,如果不合规就使用raise弹出一个异常,然后再except中抓取这个异常. 参考异常处理
江刚慧4638python怎么判读输入的是字符串还是数字 -
冉皇褚19729984412 ______ >>>type(5)<type 'int'>>>> type('adf')<type 'str'>
江刚慧4638Python 2.7版本 编写一个程序,输入一个字符串,判断字符串中是否有大小写,比如输入'!he -
冉皇褚19729984412 ______ #用所给字符串测试了一下,可行 def string_judg(string): for char in string: if char>='A' and char <='Z': print 'false' return print 'true' string=raw_input('input string:') string_judg(string)
江刚慧4638Python作业:输入一个字符,自动判断该字符是字母字符、数字字符还是其他字符; -
冉皇褚19729984412 ______ Do x = InputBox("请输入一个字符 Loop Until Len(x) = 1 Select Case Asc(LCase(x)) Case 97 To 122 MsgBox "字母字符" Case 48 To 57 MsgBox "数字字符" Case Else MsgBox "其他字符" End Select
江刚慧4638关于python判断输入的是否有值的问题 -
冉皇褚19729984412 ______ usertext, num = None, -1 while not usertext: usertext = input('Please enter some text to encrypt: ') while not (0 try: num = int(input('Please enter a num: ')) except ValueError: print('What you have entered is not an integer.')
江刚慧46382 - 9 python练习:带循环和条件判断的用户输入.使用raw... -
冉皇褚19729984412 ______ while 0 < n < 100: ..n = int(raw_input('Please input a number:')) ..print 'Wrong number.' 点代表空格.因为会把它认为“没用”的空格删掉.
江刚慧4638python 怎么判断一个字符是不是数字 -
冉皇褚19729984412 ______ def is_num(s): try: num=float(s) return True except: return False print(is_num('123.4')) print(is_num('1abc2'))
江刚慧4638Python判断字符串中是否有中文字符 -
冉皇褚19729984412 ______ 首先,在Python中字符串的表示是 用unicode编码.所以在做编码转换时,通常要以unicode作为中间编码.decode的作用是将其他编码的字符串转换成unicode编码,比如 a.decode('utf-8'),表示将utf-8编码的字符串转换成unicode编码 encode...
江刚慧4638python 编写 用户输入一个语句,判断该语句是否是循环语句 -
冉皇褚19729984412 ______ python 编写 用户输入一个语句,判断该语句是否是循环语句: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 方法一: whileTrue: ten=input("x:") try: x=eval(ten) iftype(x)==int:break except:pass 然后4102输入1653asf,没有提示...