{"id":620,"date":"2016-10-11T16:19:52","date_gmt":"2016-10-11T08:19:52","guid":{"rendered":"http:\/\/www.datarelab.com\/blog\/?p=620"},"modified":"2016-10-11T16:19:52","modified_gmt":"2016-10-11T08:19:52","slug":"dnspod%e5%8a%a8%e6%80%81ip%e8%a7%a3%e6%9e%90%e6%9b%b4%e6%96%b0%e7%a8%8b%e5%ba%8f","status":"publish","type":"post","link":"https:\/\/www.datarelab.com\/blog\/Technical_literature\/620.html","title":{"rendered":"DNSPod\u52a8\u6001IP\u89e3\u6790\u66f4\u65b0\u7a0b\u5e8f"},"content":{"rendered":"<p>\u811a\u672c\u662fPython\u5199\u7684\uff0c\u6811\u8393\u6d3e\u4e0a\u53ef\u76f4\u63a5\u8fd0\u884c\u3002<\/p>\n<pre class=\"lang:python decode:true\">#!\/usr\/bin\/env python\r\n#-*- coding:utf-8 -*-\r\n\r\nimport httplib, urllib\r\nimport socket\r\nimport time\r\n\r\nparams = dict(\r\n    login_email=\"email\", # replace with your email\r\n    login_password=\"password\", # replace with your password\r\n    format=\"json\",\r\n    domain_id=100, # replace with your domain_od, can get it by API Domain.List\r\n    record_id=100, # replace with your record_id, can get it by API Record.List\r\n    sub_domain=\"www\", # replace with your sub_domain\r\n    record_line=\"\u9ed8\u8ba4\",\r\n)\r\ncurrent_ip = None\r\n\r\ndef ddns(ip):\r\n    params.update(dict(value=ip))\r\n    headers = {\"Content-type\": \"application\/x-www-form-urlencoded\", \"Accept\": \"text\/json\"}\r\n    conn = httplib.HTTPSConnection(\"dnsapi.cn\")\r\n    conn.request(\"POST\", \"\/Record.Ddns\", urllib.urlencode(params), headers)\r\n    \r\n    response = conn.getresponse()\r\n    print response.status, response.reason\r\n    data = response.read()\r\n    print data\r\n    conn.close()\r\n    return response.status == 200\r\n\r\ndef getip():\r\n    sock = socket.create_connection(('ns1.dnspod.net', 6666))\r\n    ip = sock.recv(16)\r\n    sock.close()\r\n    return ip\r\n\r\nif __name__ == '__main__':\r\n    while True:\r\n        try:\r\n            ip = getip()\r\n            print ip\r\n            if current_ip != ip:\r\n                if ddns(ip):\r\n                    current_ip = ip\r\n        except Exception, e:\r\n            print e\r\n            pass\r\n        time.sleep(30)\r\n<\/pre>\n<p>\u66ff\u6362\u4e0a\u4f60\u7684Email\uff0c\u5bc6\u7801\uff0c\u57df\u540dID\uff0c\u8bb0\u5f55ID\u7b49\u53c2\u6570\uff0c\u5c31\u53ef\u4ee5\u8fd0\u884c\u4e86\u3002 \u4f1a\u5728\u540e\u53f0\u4e00\u76f4\u8fd0\u884c\uff0c\u6bcf\u969430\u79d2\u68c0\u67e5\u4e00\u904dIP\uff0c\u5982\u679c\u4fee\u6539\u4e86\u5c31\u66f4\u65b0IP\u3002<br \/>\n\u83b7\u5f97domain_id\u53ef\u4ee5\u7528<\/p>\n<p>curl -k https:\/\/dnsapi.cn\/Domain.List -d \"login_email=xxx&amp;login_password=xxx\"<\/p>\n<p>\u83b7\u5f97record_id\u7c7b\u4f3c<\/p>\n<p>curl -k https:\/\/dnsapi.cn\/Record.List -d \"login_email=xxx&amp;login_password=xxx&amp;domain_id=xxx\"<\/p>\n<p>\u52a0\u5165\u5f00\u673a\u81ea\u52a8\u8fd0\u884c\u7684\u65b9\u6cd5\uff1a<br \/>\n\u7f16\u8f91\/etc\/rc.local\u6587\u4ef6\uff0c\u5728\u201cexit 0\u201d\u90a3\u4e00\u884c\u524d\u9762\u52a0\u4e0a\u4e00\u884c<\/p>\n<p>su root -c \"python \/home\/pypod.py\"<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u811a\u672c\u662fPython\u5199\u7684\uff0c\u6811\u8393\u6d3e\u4e0a\u53ef\u76f4\u63a5\u8fd0\u884c\u3002 #!\/usr\/bin\/env python #-*- codin [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-620","post","type-post","status-publish","format-standard","hentry","category-Technical_literature"],"views":2205,"_links":{"self":[{"href":"https:\/\/www.datarelab.com\/blog\/wp-json\/wp\/v2\/posts\/620","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.datarelab.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.datarelab.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.datarelab.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.datarelab.com\/blog\/wp-json\/wp\/v2\/comments?post=620"}],"version-history":[{"count":0,"href":"https:\/\/www.datarelab.com\/blog\/wp-json\/wp\/v2\/posts\/620\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.datarelab.com\/blog\/wp-json\/wp\/v2\/media?parent=620"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.datarelab.com\/blog\/wp-json\/wp\/v2\/categories?post=620"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.datarelab.com\/blog\/wp-json\/wp\/v2\/tags?post=620"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}