dnspod ddns 微信推送

import requests import re import json import time #延时10秒执行防止刚开机还没联网 time.sleep(10) corp_id = '12222' #微信企业id corp_secret = '156667' #应用Secret agent_id = '1000' #应用id #获取微信token def get_access_token(corp_id, corp_secret): resp = requests.get(f'https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={corp_id}&corpsecret={corp_secret}') js = json.loads(resp.text) #print(js) if js["errcode"] == 0: access_token = js["access_token"] expires_in = js["expires_in"] return access_token, expires_in #发送微信文字消息 def wechat_push_text(agent_id, access_token, message): data = { "touser": "@all", "msgtype": 'text', "agentid": agent_id, "text": { "content": message }, "safe": 0, "enable_id_trans": 0, "enable_duplicate_check": 0, "duplicate_check_interval": 1800 } resp = requests.post(f'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={access_token}', json=data) js = json.loads(resp.text) #print(js) if js["errcode"] == 0: return js access_token, expires_in = get_access_token(corp_id, corp_secret) #域名ID domain_id = '123456' #前缀ID record_id = '12345678' #dnspod api id,key login_token = '12345,1234567890' #域名前缀 sub_domain = 'www' url = 'http://myip.ipip.net/' # ..dnspod..User-Agent headers = {'User-Agent':'RasPi Auto DDNS/1.0.0 (email@domain.com)'} #获取当前ip try: res = requests.get(url).text ip = re.findall(r'(\d+\.\d+\.\d+\.\d+)',res) ip = ip[0]if ip else '' wlan_ip = ip print('本地ip:',wlan_ip) except: sys.exit(0) #获取域名当前ip url_list = 'https://dnsapi.cn/Record.List' data_list = { 'login_token':login_token, 'format':'json', 'domain_id':domain_id, 'sub_domain':sub_domain, 'record_type':'A' } data_dns = requests.post(url=url_list,data=data_list,headers=headers).json() list_ip = data_dns["records"][0] ["value"] #判断当前ip和域名ip是否一样不一样则更新 print('域名ip:',list_ip) if wlan_ip != list_ip: url_modify='https://dnsapi.cn/Record.Modify' data_modify={ 'login_token':login_token, 'format':'json', 'domain_id':domain_id, 'record_id':record_id, 'sub_domain':sub_domain, 'value':wlan_ip, 'record_type':'A', 'record_line':'\u9ED8\u8BA4' } response_modify =requests.post(url=url_modify,data=data_modify,headers=headers).json() #匹配返回数据微信推送 code='状态码:'+response_modify['status']['code'] message='消息:'+response_modify['status']['message'] created_at='更新时间:'+response_modify['status']['created_at'] id='域名前缀ID:'+str(response_modify['record']['id']) name='域名前缀:'+response_modify['record']['name'] value='更新ip:'+response_modify['record']['value'] wx='ip更新消息:'+'\n'+code+'\n'+message+'\n'+created_at+'\n'+id+'\n'+name+'\n'+value wechat_push_text(agent_id=agent_id,access_token=access_token,message=wx) else: print('ip一样不更新')```
如果觉得我的文章对你有用,请随意赞赏