< >
Home » 树莓派入门教程 » 树莓派3-配置-DNSPod动态IP解析更新程序(用户和密码API)

树莓派3-配置-DNSPod动态IP解析更新程序(用户和密码API)

DNSPod动态IP解析更新程序

说明

  • 如果通过外网访问内网服务,但是家居的网络IP经常变化,需要绑定一个域名和动态IP

步骤

  1. 编写Python程序

    #!/usr/bin/env python
    #-- coding:utf-8 --

    import httplib, urllib
    import socket
    import time

    params = dict(
    login_email="email", # replace with your email
    login_password="password", # replace with your password
    format="json",
    domain_id=100, # replace with your domain_od, can get it by API Domain.List
    record_id=100, # replace with your record_id, can get it by API Record.List
    sub_domain="www", # replace with your sub_domain
    record_line="默认",
    )
    current_ip = None

    def ddns(ip):
    params.update(dict(value=ip))
    headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/json"}
    conn = httplib.HTTPSConnection("dnsapi.cn")
    conn.request("POST", "/Record.Ddns", urllib.urlencode(params), headers)

     response = conn.getresponse()
     print response.status, response.reason
     data = response.read()
     print data
     conn.close()
     return response.status == 200
    

    def getip():
    sock = socket.create_connection(('ns1.dnspod.net', 6666))
    ip = sock.recv(16)
    sock.close()
    return ip

    if name == 'main':
    while True:
    try:
    ip = getip()
    print ip
    if current_ip != ip:
    if ddns(ip):
    current_ip = ip
    except Exception, e:
    print e
    pass
    time.sleep(30)

  2. 如何使用,替换上你的Email,密码,域名ID,记录ID等参数,就可以运行了。 会在后台一直运行,每隔30秒检查一遍IP,如果修改了就更新IP。

  3. 获得domain_id方法

curl curl -k https://dnsapi.cn/Domain.List -d "login_email=xxx&login_password=xxx"
  1. 获得record_id方法
curl -k https://dnsapi.cn/Record.List -d "login_email=xxx&login_password=xxx&domain_id=xxx"
  1. 开机自动运行的方法,编辑/etc/rc.local文件,在“exit 0”那一行前面加上一行
su root -c "python /home/pypod.py"

纠错,疑问,交流: 请进入讨论区点击加入Q群

获取最新文章: 扫一扫右上角的二维码加入“创客智造”公众号


标签: 树莓派3dnspod, 树莓派3动态域名, 树莓派3动态ip