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 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
| __author__ = "chenjian" import os,time,re,json,time import urllib import urllib2 import cookielib
def createFile(name): fp = "data" type = ".txt" myname = name try: os.mkdir(fp) filename = fp + "/" + myname + type
except Exception, e: if os.path.exists(fp): filename = fp + "/" + myname + type print ("Had it!")
else: os.makedirs("." + fp) print("Creat in parent directory") filename = fp + "/" + myname + type
return filename
def login(): username = '你的用户名' pwd = '你的密码' url = "http://www.zhujiwu.com/api/user.php?cmd=Login&username=%s&password=%s" % (username,pwd) values = '' headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:52.0) Gecko/20100101 Firefox/52.0', 'Accept': 'application/json, text/javascript, */*; q=0.01', 'Accept-Language': 'zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3', 'Accept-Encoding': 'gzip, deflate', 'X-Requested-With': 'XMLHttpRequest', 'Referer': 'http://www.zhujiwu.com/user/login.html', 'Connection': 'keep-alive' } try: fp = createFile('cookie') print '----------'+fp c = cookielib.LWPCookieJar(fp) opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(c))
data = urllib.urlencode(values) req = urllib2.Request(url,data,headers) response = opener.open(req) responsedata = response.read() login_data = json.loads(responsedata) code = login_data['response'] if code == '200': print 'zhujiwu login sucessful' c.save(ignore_expires=True, ignore_discard=True) if c: return c
except urllib2.URLError,e: print(e)
def change(login_cookie,ip): my_id = '你的域名id' cookie_data = str(login_cookie) a = '<LWPCookieJar[<Cookie session_id=' b = '<_LWPCookieJar.LWPCookieJar[<Cookie session_id=' d = ' for .zhujiwu.com/>]>' cookie_data = cookie_data.replace(a, "") cookie_data= cookie_data.replace(b, "") cookie_data= cookie_data.replace(d, "")
change_url = "http://www.zhujiwu.com/api/domain.php?cmd=update&content=%s&id=%s&ttl=120"%(ip,my_id) change_headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:52.0) Gecko/20100101 Firefox/52.0', 'Accept': 'application/json, text/javascript, */*; q=0.01', 'Accept-Language': 'zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3', 'Accept-Encoding': 'gzip, deflate', 'X-Requested-With': 'XMLHttpRequest', 'Referer': 'http://www.zhujiwu.com/control/domain.html', 'Cookie':'session_id='+cookie_data+'; path=/; domain=zhujiwu.com', 'Connection': 'keep-alive' } change_req = urllib2.Request(url=change_url,headers=change_headers) change_response = urllib2.urlopen(change_req) change_responsedata = change_response.read() response = json.loads(change_responsedata.decode('UTF-8-SIG')) set_status_code = response['response'] print set_status_code if set_status_code == "200": print 'zhujiwu domain set sucessful' def get_local_host(): url = "http://ddns.nat123.com/" try: opener = urllib2.build_opener() req = urllib2.Request(url) response = opener.open(req) responsedata = response.read() reg = re.compile(r"Address: ([0-9]+.+[0-9]+.+[0-9]+.+[0-9]?)") match = reg.search(responsedata) Address = match.group(0) local_ip = Address.replace('Address: ','') return local_ip
except: error_str = 'some wrong ~~~~~~~' print error_str return error_str
def set_loacl_host(): print 'the listen is going....' while True: try: file = open('./data/ip.txt') file_data =file.read() curr_ip = get_local_host() if file_data != curr_ip: print 'ip has chang' cookie = login() change(cookie,curr_ip) ip_file_path = createFile('ip') ip_file = open(ip_file_path,'w') ip_file.write(curr_ip) print ('write the curr_ip: %s into ip.txt'%curr_ip) ip_file.close() file.close() except: curr_ip = get_local_host() file_path = createFile('ip') file = open(file_path,'w') file.write(curr_ip) print ('write the curr_ip: %s into ip.txt'%curr_ip) file.close() now = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())) print (now+"now ip is : %s"%curr_ip) time.sleep(60)
if __name__ == '__main__': set_loacl_host()
|