- #-*- coding:utf-8 -*-
- import urllib2
- import json
- import urllib
- import re
- import time
- def getip():
- response = urllib2.urlopen('http://ip.911cha.com/')
- test = response.read()
- ip = re.findall(r"\\d+\\.\\d+\\.\\d+\\.\\d+",test)
- return ip[0]
- def changedns():
- record_id = {}
- record_id['168906903'] = '@'
- record_id['168906977'] = 'qinglv'
- record_id['168906947'] = 'www'
- url = 'https://dnsapi.cn/Record.Ddns'
- for dnsid , name in record_id.items():
- data = {'login_token' : '12959,1aee3f6490907f32',
- 'domain_id' : '36744820',
- 'format' : 'json',
- 'record_id' : dnsid,
- 'sub_domain': name,
- 'record_line' : '默认',
- 'value' : ip
- }
- data = urllib.urlencode(data)
- req = urllib2.Request(url , data)
- res = urllib2.urlopen(req)
- html = res.read()
- print html
- def dnsip():
- url = 'https://dnsapi.cn/Record.Info'
- data = { 'login_token' : '12959,1aee3f6490907f3',
- 'domain_id' : '36744820',
- 'format' : 'json',
- 'record_id' : '168906903'
- }
- data = urllib.urlencode(data)
- req = urllib2.Request(url , data)
- res = urllib2.urlopen(req)
- test = res.read()
- dnsip = re.findall(r"\\d+\\.\\d+\\.\\d+\\.\\d+",test)
- return dnsip[0]
- running = True
- while running:
- time.sleep(100)
- ip = getip()
- dns = dnsip()
- if ip == dns:
- print '本机ip无变化'
- else:
- changedns()
- #该片段来自于http://www.codesnippet.cn/detail/2608201615000.html
来源: http://www.codesnippet.cn/detail/2608201615000.html