【树莓派】python3脚本 自动上报ip到万网域名解析

阅读:359 2019-03-19 15:07:00 来源:新网

依赖:

python3模块:

脚本如下:

importrequestsimportjsonfromaliyunsdkcore.clientimportacsclientfromaliyunsdkcore.acs_exception.exceptionsimportclientexceptionfromaliyunsdkcore.acs_exception.exceptionsimportserverexceptionfromaliyunsdkalidns.request.v20150109importupdatedomainrecordrequest,describedomainrecordinforequestdomain='*.com'rid='该域名的rid'client=acsclient("appkey","appsecret","cn-hangzhou")defip():#自己写的获取源ip服务r=requests.get('http://*.*.com/get/ip')ifr.status_code==200:returnr.textelse:returnnonedefupdate_record(cip,old):update_pi(cip)req=updatedomainrecordrequest.updatedomainrecordrequest()req.set_recordid(rid)req.set_rr(old['rr'])req.set_type(old['type'])req.set_value(cip)resp=client.do_action_with_exception(req)print(resp)returntruedefupdate_pi(cip):postdata={'ip':ip}#自己写的记录ip变化的web服务r=requests.post('http://*.*.com/pi/ip',data=postdata)returnr.text=='ok'defconnect_dns():cip=ip()ifnotcip:returnfalsereq=describedomainrecordinforequest.describedomainrecordinforequest()req.set_recordid(rid)resp=client.do_action_with_exception(req).decode('utf-8')old=json.loads(resp)ifcip==old['value']:returntrueelse:returnupdate_record(cip,old)if__name__=='__main__':connect_dns()

相关文章
{{ v.title }}
{{ v.description||(cleanHtml(v.content)).substr(0,100)+'···' }}
你可能感兴趣
推荐阅读 更多>
推荐商标

{{ v.name }}

{{ v.cls }}类

立即购买 联系客服