作者:用户yfkae7hw22 | 来源:互联网 | 2023-05-17 13:53
这期内容当中小编将会给大家带来有关如何解析树莓派加python3脚本实现自动上报ip到万网域名,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。
依赖:
python3 模块:
脚本如下:
import requests
import json
from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.acs_exception.exceptions import ClientException
from aliyunsdkcore.acs_exception.exceptions import ServerException
from aliyunsdkalidns.request.v20150109 import UpdateDomainRecordRequest, DescribeDomainRecordInfoRequest
DOMAIN = '*.com'
RID = '该域名的rid'
client = AcsClient(
"appkey",
"appsecret",
"cn-hangzhou"
)
def ip():
#自己写的获取源 IP服务
r = requests.get('http://*.*.com/get/ip')
if r.status_code == 200:
return r.text
else:
return None
def update_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)
return True
def update_pi(cip):
postdata = {'ip': ip}
#自己写的记录IP变化的web服务
r = requests.post('http://*.*.com/pi/ip', data=postdata)
return r.text == 'ok'
def connect_dns():
cip = ip()
if not cip:
return False
req = DescribeDomainRecordInfoRequest.DescribeDomainRecordInfoRequest()
req.set_RecordId(RID)
resp = client.do_action_with_exception(req).decode('utf-8')
old = json.loads(resp)
if cip == old['Value']:
return True
else:
return update_record(cip, old)
if __name__ == '__main__':
connect_dns()
然后放置在crontab 每半个小时执行一次,用来自动检测和更新
上述就是小编为大家分享的如何解析树莓派加python3脚本实现自动上报ip到万网域名了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注编程笔记行业资讯频道。