首页 >>  正文

python怎么判断是否是整数

来源:baiyundou.net   日期:2024-08-24

作者:王忘杰

全民制作人大家好,我是学习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监控+企业微信机器人告警

孟红帝3796python 中怎么判断一个数是否为1 -
柳光子18573443458 ______ 如果这个数是从输入中得到的,可以用一下判断 a = input() if a is '1': print ("Yes, input number is 1")如果在代码中,1可能是个数字,也可能是字符串,若是数字类型,直接用数字比较 a = 1 if a is 1: print ("Yes, the number is 1")

孟红帝3796python 中怎样判断是不是文件 -
柳光子18573443458 ______ 在os.path模块中有个isfile的方法,该方法可以判断是不是文件,返回True说明是文件,返回False则不是文件,下面的英文是摘自python文档 os.path.isfile(path) Return True if path is an existing regular file. This follows symbolic links, so both islink() and isfile() can be true for the same path. 用法也很简单 import os filename='/tmp/test.txt' print os.path.isfile(filename)

孟红帝3796python 判断变量是否定义 -
柳光子18573443458 ______ vars(不带参数的形式)和locals这两个内建函数返回一个当前scope内定义的所有局部变量的字典. >>> 'a' in vars() False >>> a = 1 >>> 'a' in vars() True >>> b = 2 >>> locals()

孟红帝3796Python如何判断一个进程是否存在 -
柳光子18573443458 ______ import ctypes handle = ctypes.windll.user32.GetForegroundWindow() #获得后台进程句柄 print(handle) PID = ctypes.windll.kernel32.GetProcessId(handle) #通过句柄获取pid print (PID)

孟红帝3796请教Python怎么判断一个变量是否定义了 -
柳光子18573443458 ______ 个人理解: 在对变量操作(打印,计算、判断等)之前没有对变量做赋值,那么这个变量就是没有定义的,反之则是定义了的

孟红帝3796Python3 先判断输入的是不是数字,若不是,则输出“输入无效,卿输入一个数字” -
柳光子18573443458 ______ def askNumber(): question='请输入一个数字' rep=False while not rep: try: rep =int(input(question)) except: rep=False question ='输入无效,请输入一个数字' return rep num=askNumber() if num >0: print('输入的是正数') pass elif num==0: pass else: pass

孟红帝3796用Python判断一个字符串是否是正则表达式 -
柳光子18573443458 ______ 一个简捷的方法是,1234 try: re.compile('xxx'); catch: # 如果有异常发生,说明不是合法的正则 这样是python语法来判断的.

孟红帝3796python 怎么查看进程是否存在 -
柳光子18573443458 ______ #!/usr/bin/env python import os import signal # Change this to your process name processname = 'aterm' for line in os.popen("ps xa"): fields = line.split() pid = fields[0] process = fields[4] if process.find(processname) > 0: # Kill the Process. ...

孟红帝3796python如何判断对象是否为字符串或者其他类型 -
柳光子18573443458 ______ isinstance(1,int) True isinstance('a',str) True

孟红帝3796Python中判断字符串是否是数的方法 -
柳光子18573443458 ______ 可以用字符串的方法.isalpha()判断字符串是否全部是英文字母,包含大小写,不包含数字和空格 s = 'hello there' for i in s.split(' '): print i.isalpha()

(编辑:自媒体)
关于我们 | 客户服务 | 服务条款 | 联系我们 | 免责声明 | 网站地图 @ 白云都 2024