腾讯云PythonDDNS脚本,兼顾ipv4和ipv6

分为三个文件,

.py文件为主程序,一个config.txt为配置文件,一个注释文件.txt为注解文件,因为原本是打包成软件发布,所以文件比较多且繁琐。

获取公网地址:

拨打电话:

移动:10086

电信:10000

联通:10010

转人工台,报告人工台开通ipv4或者ipv6公网地址,旧路由不支持ipv6。ipv6公网地址可能会出现部分软件访问失常或者掉速。最好是开通ipv4+ipv6,因为有实验证明,30M的ipv4,在ipv6情况下能跑40M。50M和100M同理。

 

本期讲解公网ipv4/ipv6怎么用python做DDNS

需要的材料是:

1. 拥有公网家庭网 IPv4或者IPv6

2.一台闲置的主机,小白就用windows系统,这里也只讲解windows系统

3.腾讯云账户里有域名

4.有点闲钱可以交得起电费和网费。

 

教程:

  1. 修改配置文件config.txt 怎么修改请下翻点击 需要用到的链接。

2、打开软件和注册秘钥文件,填入对应的内容,注册,如果找不到注册秘钥,可以先点击试用,每台电脑试用3次,每次60秒。

秘钥获取地址:

https://wwxq.lanzoub.com/b03pqwqxg
密码:7pxl

3、首次运行有提示!熟读后确认,马上域名自动绑定到你当前主机的公网地址。但是要能访问一般得等10分钟后才可以访问,因为TTL是600。但是在之后的访问便不需要等待10分钟,基本上是即时更新。

需要用到的网址:

查域名RecordID:

登录 - 腾讯云

变更域名解析:

登录 - 腾讯云

登录密钥:

登录 - 腾讯云

域名管理界面:

登录 - 腾讯云

域名购买界面:

域名注册购买_域名注册选购 - 腾讯云

 

 

如果自己搭建,三个可能需要用到的指令:

pip install --upgrade tencentcloud-sdk-python

pip install --upgrade pyinstaller

pyinstaller -F -i icon.ico main.py

联系QQ:1946308592

主程序代码段

import json
import requests
import time
import re
from tencentcloud.common import credential
from tencentcloud.common.profile.client_profile import ClientProfile
from tencentcloud.common.profile.http_profile import HttpProfile
from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException
from tencentcloud.dnspod.v20210323 import dnspod_client, models
from urllib.request import urlopen
import linecache
import win32api
###########################################################
# 定义ipv4获取方法
def getipv4():
    url = 'https://ddns.oray.com/checkip'
    # ipv4 的直链
    headers = {'User_Agent': "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36 Edg/109.0.1518.78",}
    requests.packages.urllib3.disable_warnings()
    resp = requests.get(url, headers=headers, verify=False)
    # print(resp.text)
    # 访问url
    resp2 = re.findall(r'(?:\d+\.){3}\d+',resp.text)
    html_ipv4 = resp2[0]
    # 读 且 转字符串格式显示
    print(html_ipv4)
    return html_ipv4
# 定义ipv6获取方法
def getipv6():
    url = 'https://6.ipw.cn/'
    # ipv6 的直链
    headers = {
        'User_Agent': "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36 Edg/109.0.1518.78",
    }
    requests.packages.urllib3.disable_warnings()
    resp = requests.get(url, headers=headers, verify=False)
    # 访问url
    html_ipv6 = resp.text
    # 读 且 转字符串格式显示
    print(html_ipv6)
    return html_ipv6
###########################################################


###########################################################
# 定义休眠函数,每隔60秒请求一次
def sleep_func():
    time.sleep(600)
    # 休眠60秒
###########################################################


###########################################################
# 指出全局变量log为前提
global log
# 设置log为布尔值True
log = True
###########################################################


###########################################################
# 读取config.txt以匹配源码
global config_SecretId, config_SecretKey, config_Domain, config_SubDomain, config_RecordType, config_Value, config_RecordId
config_SecretId, config_SecretKey, config_Domain, config_SubDomain, config_RecordType, config_Value, config_RecordId = None,None,None,None,None,None,None
global getipValue
getipValue = None

global ACI_str,ACT_str2,ACT_str_default
ACI_str = '画中云Joy'
ACI_str2 = 1946308592
ACI_str_default = '作者:哔哩哔哩Up:画中云Joy'
#ACI
global ACI_set
ACI_set = True
def config():
    global config_SecretId, config_SecretKey, config_Domain, config_SubDomain, config_RecordType, config_Value, config_RecordId
    configname = 'config.txt'
    config_SecretId = linecache.getline(configname,4)
    config_SecretKey = linecache.getline(configname,7)
    config_Domain = linecache.getline(configname,10)
    config_SubDomain = linecache.getline(configname,13)
    config_RecordType = linecache.getline(configname,16)
    config_Value = linecache.getline(configname,19)[:-1]
    config_RecordId = linecache.getline(configname,22)

    config_RecordId = int(config_RecordId)
    global getipValue
    #声明全局变量getipValue用于主程序获取判断方法

    if config_Value == 'ipv4':
        getipValue = getipv4()
        print('配置文件中记录值已自动获取公网ipv4')
        return getipValue

    elif config_Value == 'ipv6':
        getipValue = getipv6()
        print('配置文件中记录值已自动获取公网ipv6')
        return getipValue

    #打印一遍获取的信息
    # print(config_SecretId)
    # print(config_SecretKey)
    # print(config_Domain)
    # print(config_SubDomain)
    # print(config_RecordType)
    # print(config_Value)
    # print(config_RecordId)


###########################################################


###########################################################
def teacher():
    configname = '注释文件.txt'
    config_First = linecache.getline(configname, 3)[:-1]
    if config_First == '是':
        win32api.MessageBox(0, f'首次运行,当前配置如下,如若有误请自行配置\n'
                            f'或者联系作者QQ 1946308592\n'
                            f'SecretId:{config_SecretId}  # 这项在腾讯云个人信息里面的访问密钥管理\n'
                            f'SecretKey:{config_SecretKey}  # 这项在腾讯云个人信息里面的访问密钥管理\n'
                            f'Domain:{config_Domain}  # 主域名,如果映射域名为abc.123.com,这里填123.com\n'
                            f'SubDomain:{config_SubDomain}  # 子域名头部,如果域名为abc.123.com,这里填abc\n'
                            f'RecordType:{config_RecordType}  # A为映射ipv4,AAAA为映射ipv6\n'
                            f'Value:{config_Value}  # 该值如果RecordType为A填ipv4,为AAAA填ipv6\n'
                            f'RecordId:{config_RecordId}  # 这项在API Explorer中的DNSPod查找\n'
                            f'不懂的可以到哔哩哔哩搜我的视频,我名字叫做画中云Joy,\n'
                               f'或者加我QQ 1946308592'
                            , '作者的提示')
        with open(configname, mode='w+', encoding='UTF-8') as f:
            lines = ['###########################################################\n',
                     '首次运行是否进行配置文件提示:\n',
                     '否\n',
                     '###########################################################\n',
                     'SecretId			# 这项在腾讯云个人信息里面的访问密钥管理\n',
                     'SecretKey			# 这项在腾讯云个人信息里面的访问密钥管理\n',
                     'Domain				# 主域名,如果映射域名为abc.123.com,这里填123.com\n',
                     'SubDomain			# 子域名头部,如果域名为abc.123.com,这里填abc\n',
                     'RecordType			# A为映射ipv4,AAAA为映射ipv6\n',
                     'Value				# 该值如果RecordType为A填ipv4,为AAAA填ipv6\n',
                     'RecordId			# 这项在API Explorer中的DNSPod查找\n',
                     '###########################################################\n',
                     '不懂的可以到哔哩哔哩搜我的视频,我名字叫做画中云Joy,或者加我QQ 1946308592\n']
            f.seek(0)
            f.writelines(lines)
            print(f.read())
            f.close()
    else:
        print('你已经不是第一次运行这个软件了!请确认你已熟知配置方法!')


###########################################################


###########################################################
#Anti-counterfeiting identification
def ACI():
    global ACI_str,ACT_str2,ACT_str_default
    global ACI_set
    ACI_set = False
    QQ = 1946308592
    Admin = '画中云Joy'
    configname = 'config.txt'
    ACI_text = linecache.getline(configname,25)[:-1]
    print(f'###########################################################\n'
          f'{ACI_str_default}\n'
          f'{QQ}\n'
          f'###########################################################')
    if ACI_str_default == ACI_text:
        win32api.MessageBox(0,f'防伪标识验证成功!请使用!\n'
                              f'作者QQ: {QQ}\n'
                              f'BiliBili:{Admin}\n'
                              f'如果闪退请注意配置文件config.txt是否配置正确!','防伪标识验证')
        config()
        teacher()
        ACI_set =True
    elif ACI_str == ACI_text or ACI_str2 ==ACI_text:
        win32api.MessageBox(0, f'防伪标识验证成功!请使用!\n'
                               f'作者QQ: {QQ}\n'
                               f'BiliBili:{Admin}\n'
                               f'如果闪退请注意配置文件config.txt是否配置正确!','防伪标识验证')
        config()
        teacher()
        ACI_set = True
    else :
        win32api.MessageBox(0,f'防伪标识验证失败!'
                                 f'\n请将同目录下的config.txt中的防伪标识下方内容修改为:'
                                 f'\n1946308592'
                                 f'\n或者'
                                 f'\n画中云Joy',
                                    '防伪标识验证'
                                             )
        ACI_set = False
###########################################################


###########################################################
def tryseting():
    try:
        # 实例化一个认证对象,入参需要传入腾讯云账户secretId,secretKey,此处还需注意密钥对的保密
        # 密钥可前往https://console.cloud.tencent.com/cam/capi网站进行获取
        cred = credential.Credential(f"{config_SecretId.strip()}", f"{config_SecretKey.strip()}")
        # 实例化一个http选项,可选的,没有特殊需求可以跳过
        httpProfile = HttpProfile()
        httpProfile.endpoint = "dnspod.tencentcloudapi.com"

        # 实例化一个client选项,可选的,没有特殊需求可以跳过
        clientProfile = ClientProfile()
        clientProfile.httpProfile = httpProfile
        # 实例化要请求产品的client对象,clientProfile是可选的
        client = dnspod_client.DnspodClient(cred, "", clientProfile)

        # 实例化一个请求对象,每个接口都会对应一个request对象
        req = models.ModifyRecordRequest()
        params = {
            "Domain": f"{config_Domain.strip()}",
            "SubDomain": f"{config_SubDomain.strip()}",
            "RecordType": f"{config_RecordType.strip()}",
            "RecordLine": "默认",
            "Value": f'{getipValue}',
            "RecordId":config_RecordId,
        }
        req.from_json_string(json.dumps(params))

        # 返回的resp是一个ModifyRecordResponse的实例,与请求对象对应
        resp = client.ModifyRecord(req)
        # 输出json格式的字符串回包
        print(resp.to_json_string())
        # 再次声明log全局变量为后面while语句设置单次循环使用
        global  log
        if config_Value == 'ipv4':
            while log is True:
                with open('ip-log.txt', 'a') as f:
                    t = time.asctime(time.localtime(time.time()))
                    f.write(t)
                    f.write('\t')
                    f.write(getipv4())
                    f.write('\t')
                    f.write('success!')
                    f.write('\n')
                    log = False
        elif config_Value == 'ipv6':
            while log is True:
                with open('ip-log.txt', 'a') as f:
                    t = time.asctime(time.localtime(time.time()))
                    f.write(t)
                    f.write('\t')
                    f.write(getipv6())
                    f.write('\t')
                    f.write('success!')
                    f.write('\n')
                    log = False
        else :
            print('你似乎遇到了bug?')
    except TencentCloudSDKException as err:
        print(err)
        with open('ip-log.txt', 'a') as f:
            t = time.asctime(time.localtime(time.time()))
            f.write(t)
            f.write('\t')
            f.write('faild!')
            f.write('\n')
###########################################################


if __name__ == '__main__':
        ACI()
        while ACI_set == True:
            config()
            tryseting()
            sleep_func()

        #感谢使用画中云Joy的Windows 腾讯云DDNS Python插件!

 

config.txt文本段:

#配置文件请按注释文件改!
###########################################################
SecretId:
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
###########################################################
SecretKey:
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
###########################################################
Domain:
35a.club
###########################################################
SubDomain:

###########################################################
RecordType:
A
###########################################################
Value:
ipv4
###########################################################
RecordId:
XXXXXXXXXX
###########################################################
防伪标识:
作者:哔哩哔哩Up:画中云Joy
###########################################################

 

注释文件.txt内容段:

###########################################################
首次运行是否进行配置文件提示:
是
###########################################################
SecretId			# 这项在腾讯云个人信息里面的访问密钥管理
SecretKey			# 这项在腾讯云个人信息里面的访问密钥管理
Domain				# 主域名,如果映射域名为abc.123.com,这里填123.com
SubDomain			# 子域名头部,如果域名为abc.123.com,这里填abc
RecordType			# A为映射ipv4,AAAA为映射ipv6
Value				# 该值如果RecordType为A填ipv4,为AAAA填ipv6
RecordId			# 这项在API Explorer中的DNSPod查找
###########################################################
不懂的可以到哔哩哔哩搜我的视频,我名字叫做画中云Joy,或者加我QQ 1946308592

也可以自己直接下载取用,文件帖子:

(8条消息) 腾讯云ddns-python-ipv4+ipv6exe脚本文件第二版-Python文档类资源-CSDN文库

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值