1.替换上你DNSPOD的Email,密码,就可以运行了 2.把/usr/bin/python /home/pi/pypod.py 放到/etc/rc.local中,开机即可自动运行 3.后台一直运行,每隔600秒检查一遍IP是否改变,如果修改了就更新IP。
我的树莓派搭建的动态DNS网站 http://pi.mithinking.com
1.替换上你DNSPOD的Email,密码,就可以运行了 2.把/usr/bin/python /home/pi/pypod.py 放到/etc/rc.local中,开机即可自动运行 3.后台一直运行,每隔600秒检查一遍IP是否改变,如果修改了就更新IP。
我的树莓派搭建的动态DNS网站 http://pi.mithinking.com
| #!/usr/bin/env python | |
| #-*- coding:utf-8 -*- | |
| # 2013/5/18 By fallenleaf | |
| import httplib, urllib | |
| import socket | |
| import time | |
| import json | |
| params = dict( | |
| login_email="email", # replace with your email | |
| login_password="password", # replace with your password,WDH | |
| format="json", | |
| domain_id=0, # auto get domain_id | |
| record_id=0, # auto get record_id | |
| domain_name="mithinking.com", # replace with your domain name | |
| sub_domain="pi", # replace with your sub_domain | |
| record_type="A", # record type | |
| value = '', # auto get the IP | |
| record_line="默认" | |
| ) | |
| current_ip = None | |
| def get_domain_id(): | |
| keys = ["login_email", "login_password","format"] | |
| domain_param = { k:v for k,v in params.iteritems() if k in keys } | |
| headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/json"} | |
| conn = httplib.HTTPSConnection("dnsapi.cn") | |
| conn.request("POST", "/Domain.List", urllib.urlencode(domain_param), headers) | |
| response = conn.getresponse() | |
| print response.status, response.reason | |
| data = json.loads(response.read()) | |
| print data | |
| domain_id = 0 | |
| for each in data['domains']: | |
| if each['name'].decode('ascii')==params['domain_name']: | |
| domain_id = each['id'] | |
| break | |
| conn.close() | |
| return domain_id | |
| def get_sub_domain_id(): | |
| keys = ["login_email", "login_password","format","sub_domain","domain_id"] | |
| sub_domain_param = { k:v for k,v in params.iteritems() if k in keys } | |
| headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/json"} | |
| conn = httplib.HTTPSConnection("dnsapi.cn") | |
| conn.request("POST", "/Record.List", urllib.urlencode(sub_domain_param), headers) | |
| response = conn.getresponse() | |
| print response.status, response.reason | |
| data = json.loads(response.read()) | |
| print data | |
| sub_domain_id = 0 | |
| if data['status']['code'].decode('ascii') == '1': | |
| for each in data['records']: | |
| if each['name'].decode('ascii')==params['sub_domain']: | |
| sub_domain_id = each['id'] | |
| break | |
| conn.close() | |
| return sub_domain_id | |
| def add_sub_domain(): | |
| keys = ["login_email", "login_password","format","sub_domain","domain_id","record_type","record_line","value"] | |
| sub_domain_param = { k:v for k,v in params.iteritems() if k in keys } | |
| headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/json"} | |
| conn = httplib.HTTPSConnection("dnsapi.cn") | |
| conn.request("POST", "/Record.Create", urllib.urlencode(sub_domain_param), headers) | |
| response = conn.getresponse() | |
| print response.status, response.reason | |
| data = json.loads(response.read()) | |
| print data | |
| conn.close() | |
| sub_domain_id = 0 | |
| if data['status']['code'].decode('ascii') == '1': | |
| sub_domain_id = data['record']['id'].decode('ascii') | |
| conn.close() | |
| return sub_domain_id | |
| def ddns(ip): | |
| keys = ["login_email", "login_password","format","sub_domain","domain_id","record_line","value","record_id"] | |
| sub_domain_param = { k:v for k,v in params.iteritems() if k in keys } | |
| headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/json"} | |
| conn = httplib.HTTPSConnection("dnsapi.cn") | |
| conn.request("POST", "/Record.Ddns", urllib.urlencode(sub_domain_param), 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__': | |
| params['value'] = getip() | |
| domain_id = get_domain_id(); | |
| print params['domain_name']+" domain ID: "+str(domain_id) | |
| if domain_id<>params['domain_id']: | |
| params['domain_id'] = domain_id | |
| record_id = get_sub_domain_id() | |
| print params['sub_domain']+" record ID: "+str(record_id) | |
| if record_id ==0: | |
| record_id = add_sub_domain() | |
| print params['sub_domain']+" new record ID: "+str(record_id) | |
| params['record_id'] = record_id | |
| while True: | |
| try: | |
| ip = getip() | |
| if current_ip != ip: | |
| if ddns(ip): | |
| current_ip = ip | |
| print 'current IP is '+ current_ip | |
| except Exception, e: | |
| print e | |
| pass | |
| time.sleep(600) |