requests+post请求
作者:王忘杰
全民制作人大家好,我是学习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监控+企业微信机器人告警
柴淑鲁5181python requests获取状态吗 -
栾狡矿15744621846 ______ 1.使用postman的时候,输入url和参数,调用post方法,接口会返回数据 2.然后我就使用了Python的requests去实现 3.r = requests.request('POST',req,data=value)
柴淑鲁5181在requests库的get方法中,timeout参数用来约定请求的超时时间,请问该参数的单位 -
栾狡矿15744621846 ______ 超时你可以告诉 requests 在经过以 timeout 参数设定的秒数时间之后停止等待响应:>>> requests.get('http://github.com', timeout=0.001)Traceback (most recent call last): File "", line 1, in requests.exceptions.Timeout: HTTPConnection
柴淑鲁5181python怎样保证requests抓取下的text的编码格式正确 -
栾狡矿15744621846 ______ requests对象的get和post方法都会返回一个Response对象,这个对象里面存的是服务器返回的所有信息,包括响应头,响应状态码等.其中返回的网页部分会存在.content和.text两个对象中.两者区别在于,content中间存的是字节码,而text中...
柴淑鲁5181python 的 requests 利用代理请求网址不成功.是不是我代理写错了 -
栾狡矿15744621846 ______ request使用代理的方法很简单,你可以参考一下: import requests proxies = { "http": "http://10.10.1.10:3128", "https": "http://10.10.1.10:1080", } requests.get("http://example.org", proxies=proxies)如果解决了您的问题请采纳! 如果未解决请继续追问
柴淑鲁5181python requests怎么设置链接超时 -
栾狡矿15744621846 ______ 方法里有timeout参数,单位是秒:requests.get(timeout=60)
柴淑鲁5181python里面的requests 怎么读取file的路径 -
栾狡矿15744621846 ______ import osfilepath='/usr/bin'files=os.listdir(filepath) #获取filepath路径下的所有文件列表filename='aa.txt'fullname=(os.sep).join([filepath,filename])with open(fullname) as f: s=f.read()
柴淑鲁5181python requests post 参数重名 -
栾狡矿15744621846 ______ 不用dict,而是用tuple list.实例如下:post_data = [('a', 1), ('a', 2)] requests.post(base_url, data=post_data)