首页 > 代码库 > 使用dnspodapi 实现ddns,适用于树莓派

使用dnspodapi 实现ddns,适用于树莓派

python版本(适用于树莓派)

 1 #!/usr/bin/env python 2 #-*-coding:utf-8-*-  3 import httplib,urllib 4 import json 5 import re,urllib2 6 import time 7  8 class Getmyip: 9     def getip(self):10         try:11             myip = self.visit("http://www.whereismyip.com/")12 #           return myip13         except:14             try:15                 myip = self.visit("http://www.bliao.com/ip.phtml")16 #               return myip17             except:18                 try:19                     myip = self.visit("http://www.whereismyip.com/")20 #                   return myip21                 except:   22                     myip = "So sorry!!!"23         return myip24    25     def visit(self,url):26         opener = urllib2.urlopen(url)27         if url == opener.geturl():28             str = opener.read()29             asd=re.search(\d+\.\d+\.\d+\.\d+,str).group(0)30             return asd31 32 def setdnspod(newip):34     params = login_email=xxxxx&login_password=xxxxx&format=json&domain_id=16415545&record_id=67237927&record_line=默认&sub_domain=www&value=http://www.mamicode.com/+ newip +&record_type=A35     header = {"Content-type": "application/x-www-form-urlencoded",    36              "Accept": "text/plain"}    37     conn = httplib.HTTPSConnection("dnsapi.cn")38     #获取api版本信息39     #conn.request("POST","/Info.Version",params,header)40     #获取domains列表41     #已知"id": xxxx,"name": "xxxx"42     #conn.request("POST","/Domain.List",params,header)43     #设置ddns44     conn.request("POST","/Record.Modify",params,header)45     #获取记录列表46     #已知 "id": "xxxx", "name": "blog",      "id": "xxxx","name": "www",47     #conn.request("POST","/Record.List",params,header)48     response = conn.getresponse()49     #print response.status, response.reason 50     data =http://www.mamicode.com/ response.read()  51     #print(data)52     conn.close()53     s = json.loads(data)54     return s["status"]["code"],data55 56 if __name__ == __main__:  57     getmyip = Getmyip()  58     localip=0  59     while(1):  60         time.sleep(1)  61         tm_str_start = time.strftime(%Y-%m-%d %H:%M:%S,time.localtime(time.time()))62         print(tm_str_start+\n)63         localtmp = getmyip.getip()64         if (localtmp == None):        65             print(GET LOCAL IP ERROR!\n)66         else:67             if (localip != localtmp):68                 localip = localtmp69                 code,data =http://www.mamicode.com/ setdnspod(localip)70                 if (code == 1):71                     print(set new ip success: + localip +\n)72                 else:73                     print(set new ip failed: + code + : + data + \n)74             else:75                 print(new ip == old ip+:+localip+:+localtmp+\n)76 77  

仅作记录