首页 >>  正文

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":"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监控+企业微信机器人告警

贡虏章3654python字符串判断是否有数字 -
步通呼15252177990 ______ 用正则来判断,正则表达式可以这么写[0-9],表示只要匹配到0-9这几个数字,就表示字符串中有数字

贡虏章3654python 判断是否是数字 -
步通呼15252177990 ______ python的字符串类型有一个属性isdigit可以判断是否为数字 假设设置了一个变量a,则选择把a转换成字符串,再判断 代码如下: a=123 print(str(a).isdigit()) 如果变量a是数字,则返回True,否则返回False

贡虏章3654Python中判断字符串是否是数的方法 -
步通呼15252177990 ______ 可以用字符串的方法.isalpha()判断字符串是否全部是英文字母,包含大小写,不包含数字和空格 s = 'hello there' for i in s.split(' '): print i.isalpha()

贡虏章3654python判断字符串是否为数字? -
步通呼15252177990 ______ 判定是否为数字方法一: try: float(s) return True exceptValueError: pass try: importunicodedata unicodedata.numeric(s) return True except (TypeError , ValueError): pass return False 方法二:普通十进制正常处理 对于非十进制还需要更复杂的方法...

贡虏章3654python 判断每行第一个数和最后一个数的尾数是否相等,输出结果25,24,23,02,05,06,6513,14,15,16,66,53,2203,60,04,06,07,09,2321,22,23,02,05,06,08读取... -
步通呼15252177990 ______[答案] numfile=open('num.txt') resultfile=open('num2.txt','w') numlist=[] for line in numfile: line=line.strip() numlist=line.split(',') if numlist[0][-1]==numlist[-1][-1]: line=line+" "+numlist[0]+", "+numlist[-1] resultfile.write(line+'\n') numfile.close() resultfile.close()

贡虏章3654请问python编程中怎么用正则表达式来编写一个程序,从而判断输入的数是否为浮点数.求高手指教. -
步通呼15252177990 ______ 1. 程序如下: import re number = str(input("Please input a number:")) pattern = re.compile(r'^[-+]?[0-9]+\.[0-9]+$') match = pattern.match(number) if match: print "Number is a float." else: print "Number is not a float." 2. 关于这个正则表达...

贡虏章3654python怎样判断输入了多少个数字 -
步通呼15252177990 ______ # 使用type函数很简单的就能判断 >>> a=1 >>> b=1.2 >>> c="hello" >>> print(type(a),type(b),type(c)) (, , ) >>> if type(a) == int: print 0 0 >>> if type(b) == int: print 0 >>>

贡虏章3654python 判断每行第一个数和最后一个数的尾数是否相等,输出结果 -
步通呼15252177990 ______ numfile=open('num.txt') resultfile=open('num2.txt','w') numlist=[] for line in numfile: line=line.strip() numlist=line.split(',') if numlist[0][-1]==numlist[-1][-1]: line=line+" "+numlist[0]+", "+numlist[-1] resultfile.write(line+'\n') numfile.close() resultfile.close()

贡虏章3654python 输入一个正整数后,判断含有几个奇数数字和偶数数字 -
步通呼15252177990 ______ con = { } data = raw_input( 'input:' ) con[ 'singular' ] = len( [ x for x in data if int( x ) % 2 ] ) con[ 'even' ] = len( data ) - con[ 'singular' ] print( con )

贡虏章3654python numpy如何查询数组是否有某个数的总个数? -
步通呼15252177990 ______ import numpy as np a = np.ones((4,5))print(a)print(np.sum(a==1)) 假定数组为a 可以先试用a==某个数,转换为一个包含True或者False的数字,等于该树则为True,不等于则为False True又可以当作1,False可以当作0 使用np.sum求和可以得到等于该数的总个数

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