首页 >>  正文

python怎么判断是不是数字

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

作者:王忘杰

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

宦红乐3958如何判断输入的字符串是不是数字 -
邴凭达15644371334 ______ 可以判断字符串中的字符是否在0-9之间#include <stdio.h> int isNumber(const char* str) { char* p = str; while(*p) { if(*p <'0' || *p > '9') return 0 ++p; } return 1; } int main() { char str[100]={0}; scanf("%s", str); printf("%s %s是数字\n", str, isNumber(str)?" ":"不"); }

宦红乐3958Python如何判断一个进程是否存在 -
邴凭达15644371334 ______ import ctypes handle = ctypes.windll.user32.GetForegroundWindow() #获得后台进程句柄 print(handle) PID = ctypes.windll.kernel32.GetProcessId(handle) #通过句柄获取pid print (PID)

宦红乐3958python 怎么查看进程是否存在 -
邴凭达15644371334 ______ #!/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. ...

宦红乐3958怎样使用 Python 来判断一个路径是否存在判 -
邴凭达15644371334 ______ 需要导入OS模块. 判断C:\有没有test目录. 如果不存在则建立一个,代码如下: import os if not os.path.exists(r"C:\test"): os.mkdir(r"C:\test")

宦红乐3958python 如何判断 abc - def - ghi - 1 最后一位是不是数字 -
邴凭达15644371334 ______[答案] s = 'abc-def-ghi-1' print s[-1].isdigit()

宦红乐3958python 判断一个数是不是整数 -
邴凭达15644371334 ______ >>> '1'.isdigit() True>>> >>> '1.1'.isdigit() False>>> >>> 'a'.isdigit() False>>>

宦红乐3958Python判断以什么结尾以什么开头 -
邴凭达15644371334 ______ 直接使用 if 语句,后面接入判断条件即可 注:在 python 中,True 为真,False 为假pass 为什么也不做,直接跳过 if True: pass if False: pass

宦红乐3958python 如何判断一行里是否含有英文字母 -
邴凭达15644371334 ______ # !/usr/bin/python3.4 # -*- coding: utf-8 -*- import re a = "8a2656" b = "1514561A1321" c = "15465461654" d = "afgwgw" r = re.compile(r'^[a-zA-Z]') for item in d: result = r.match(item) if result != None: print("包含字母:" + result.group()) else: print("不包含字母")包含字母:a 包含字母:f 包含字母:g 包含字母:w 包含字母:g 包含字母:w

宦红乐3958python 怎么判断一个字符是不是数字 -
邴凭达15644371334 ______ def is_num(s): try: num=float(s) return True except: return False print(is_num('123.4')) print(is_num('1abc2'))

宦红乐3958Python中如何判断一个对象是否为函数? -
邴凭达15644371334 ______ >>> x = lambda x: x**2>>> type(x)>>> class y:... pass...>>> m = y()>>> type(y)>>> type(m)

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