-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathddns.py
More file actions
85 lines (77 loc) · 2.58 KB
/
ddns.py
File metadata and controls
85 lines (77 loc) · 2.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/usr/bin/env python
#coding:utf-8
# Author: syshack --<[email protected]>
# Purpose: yet another pyddns
# Created: 2013/3/19
import socket
import httplib, urllib
try: import json
except: import simplejson as json
import re
class ApiCn:
def __init__(self, email, password, **kw):
self.base_url = "dnsapi.cn"
self.params = dict(
login_email=email,
login_password=password,
format="json",
)
self.params.update(kw)
self.path = None
def request(self, **kw):
self.params.update(kw)
if not self.path:
"""Class UserInfo will auto request path /User.Info."""
name = re.sub(r'([A-Z])', r'.\1', self.__class__.__name__)
self.path = "/" + name[1:]
conn = httplib.HTTPSConnection(self.base_url)
headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/json", "User-Agent": "ddns-python/v1.0 ([email protected])"}
conn.request("POST", self.path, urllib.urlencode(self.params), headers)
response = conn.getresponse()
data = response.read()
conn.close()
ret = json.loads(data)
if ret.get("status", {}).get("code") == "1":
return ret
else:
raise Exception(ret)
__call__ = request
class DomainList(ApiCn):
pass
class _DomainApiBase(ApiCn):
def __init__(self, domain_id, **kw):
kw.update(dict(domain_id=domain_id))
ApiCn.__init__(self, **kw)
class RecordList(_DomainApiBase):
pass
class RecordDdns(_DomainApiBase):
def __init__(self, record_id, sub_domain,record_line, **kw):
kw.update(dict(
record_id=record_id,
sub_domain=sub_domain,
record_line=record_line,
))
_DomainApiBase.__init__(self, **kw)
def ddns():
email = "你的账户"
password = "你的密码"
domain = "你的域名"
sub_domain = "你的子域名"
record_type = "A"
record_line='默认'
api = DomainList(email=email, password=password)
for d in api().get('domains'):
if d['name']==domain:
domain_id=d['id']
api=RecordList(domain_id,email=email, password=password)
for r in api().get('records'):
if r['name']==sub_domain and r['type']==record_type:
record_id=r['id']
try:
api = RecordDdns(record_id,sub_domain,record_line,domain_id=domain_id,email=email, password=password)
print api()
print "Update Sucess!"
except Exception,e:
print e
if __name__ == '__main__':
ddns()