借助Python语言实现DDNS

最近家里捡了一台电脑,想放到家里做服务器,大家都知道宽带需要DDNS,我发现Oray的DDNS好像没办法用,所以我就决定自己写一个。

我首先到百度上搜了一下:发现了这个http://v2ex.com/t/200342:Cloudxns DDNS 更新脚本_基于 Python。发现这个脚本可以为我所用。于是我就用上了这个脚本作为Base。
最后写出了一个脚本:Code如下:

"""Powered By Jack---http://renjikai.com/
   Based On http://v2ex.com/t/200342 GPU"""

import urllib,time,json,hashlib,requests
from time import gmtime

"""Get Correct IP"""
params = urllib.urlencode({'ip': 'myip'})
f = urllib.urlopen("http://ip.taobao.com/service/getIpInfo2.php", params)
ddata=json.loads(f.read())
wan_ip = ddata['data']['ip']

"""Some Settings You Need To Set"""
api_url = "https://www.cloudxns.net/api2/record/238740"
api_key = "a7fe5ecbb823e7b0c968b2a885a18e98"
api_secret = "8575d2316d50406d"
Domain_ID = 11874
ttl = 120
line_id = 1
host = "home"
Domain_type = "A"

request_data = {
"domain_id": Domain_ID,
"host": host,
"value": wan_ip,
"type": Domain_type,
"ttl": ttl,
"line_id": line_id
}

request_time = time.strftime("%a, %d %b %Y %H:%M:%S GMT", gmtime())
hmac = hashlib.md5(api_key+api_url+str(json.dumps(request_data))+request_time+api_secret).hexdigest()
headers = {
'API-KEY' : api_key,
'API-REQUEST-DATE' : request_time,
'API-HMAC' : hmac,
'API-FORMAT' : 'json'
}
""" ignore urlib3 warnings """
requests.packages.urllib3.disable_warnings()
r = requests.put(url=api_url,headers=headers,data=json.dumps(request_data),verify=False)
response = r.json()
response_data = response[u'data']
print response_data
exit()

感觉还是挺好的。脚本下载地址:http://bucket.renjikai.com/getIP.py

借助Python语言实现DDNS》有9个想法

发表评论

电子邮件地址不会被公开。 必填项已用*标注