吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 2217|回复: 7
收起左侧

[Python 转载] ddnspod 自动更新ddns

  [复制链接]
话痨司机啊 发表于 2022-8-3 10:04
本帖最后由 话痨司机啊 于 2022-8-3 13:22 编辑

对ddns pod进行重写,新的代码条理清晰,便于理解,有兴趣的可以学习一下(注:没测试,逻辑上没问题,如果有问题可以自己改源码),保存为pyw可以后台执行
原来代码:DDNS_DNSPOD 亲测可用 - 『编程语言区』 - 吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

[Python] 纯文本查看 复制代码
from pathlib import Path
from threading import Timer

import requests
from loguru import logger
from retrying import retry

ip_url = "http://www.3322.org/dyndns/getip"
recordid_url = "https://dnsapi.cn/Record.List"
ddns_url = "https://dnsapi.cn/Record.Ddns"
ip_list = []

def init_parameter():
    """初始化参数"""
    global logger
    path = lambda filename:Path(__file__).parent.joinpath(filename)
    logger.add(path('ddns_record.log'))
    
@retry(tries=5)
def requestToFunction(url,method='GET',data=None):
    """请求函数,返回结果文本"""
    if method == 'GET':
        res = requests.get(url,timeout=10)
        res.encoding = 'utf8'
        return res.text
    else:
        res = requests.post(url,data=data,timeout=10)
        res.encoding = 'utf8'
        return res.text
    
def get_record_id(recordid_url,domain_id,login_token):
    """获取record_id"""
    formdata = {
        'domain_id': domain_id,
        'login_token': login_token
    }
    text = requestToFunction(recordid_url,method='POST',data=formdata)
    res_json = eval(text)
    try:
        record_id = res_json.get('records')[0]['id']
        return record_id
    except Exception as e:
        logger.error(f'获取record_id失败,失败原因{e}')
 
def ddns_record(ddns_url,domain_id,login_token,ip,record_id):
    """更新ddns"""
    formdata = {
            'domain_id': domain_id,
            'login_token': login_token,
            'record_line': '默认',
            'record_line_id': '10=1',
            'value': ip,
            'record_id': record_id
        }
    text = requestToFunction(ddns_url,method='POST',data=formdata)
    res_json = eval(text)
    if (res_json['status']['code'] == '1'):
        logger.info(res_json['status']['message'], '域名解析已更改为' + ip)

def main(domain_id,login_token):
    """主逻辑,检测IP变化并更新ddns"""
    record_id = get_record_id(recordid_url,domain_id,login_token)
    assert record_id
    ip = requestToFunction(ip_url)
    if ip_list is not None:
        on_a = ip_list.pop(0)
        if ip != on_a:
            ddns_record(ddns_url,domain_id,login_token,ip,record_id)
        else:
            logger.info('ip 未更改')
    else:
        ddns_record(ddns_url,domain_id,login_token,ip,record_id)
    ip_list.append(ip)
    
def cycle_perform(times,domain_id,login_token):
    """循环检测函数"""
    main(domain_id,login_token)
    t = Timer(times,cycle_perform,args=(times,domain_id,login_token)) # 每隔15分钟检测一次
    t.start()
    
if __name__ == '__main__':
    init_parameter()
    domain_id,login_token = "","" # 填入从[url=http://www.ddnspad.com]www.ddnspad.com[/url]上获取的domain_id,login_token
    cycle_perform(900,domain_id,login_token)

免费评分

参与人数 2吾爱币 +8 热心值 +2 收起 理由
苏紫方璇 + 7 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!
lcg2014 + 1 + 1 用心讨论,共获提升!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

wesley1224 发表于 2022-8-3 11:07
支持开源,共同进步学习。
lcg2014 发表于 2022-8-3 11:28
askadamo 发表于 2022-8-3 13:41
one_cube 发表于 2022-8-3 14:25
感谢分享!
xq2581 发表于 2022-8-3 15:04
必须顶 ,这个不错
iawyxkdn8 发表于 2022-8-3 15:57
楼主,辛苦了,谢谢分享!
krobelus 发表于 2022-8-3 16:51
支持开源,共同进步学习。
virsnow 发表于 2022-9-30 18:47
高手,学习了
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-5-17 03:43

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表