【代码】
#encoding=utf-8 #author: walker #date: 2015-08-05 #function: 通过断开连接时TP-LINK重新拨号,以切换IP #memo:此代码经过python3.4测试(适用于TL-WR847N) import base64, requests, traceback def ChangeIP(): ip = ‘192.168.1.1‘ user = ‘admin‘ pwd = ‘admin‘ desturl = ‘http://‘ + ip + ‘/userRpm/StatusRpm.htm?Disconnect=%B6%CF%20%CF%DF&wan=1‘ auth = ‘Basic ‘ + base64.b64encode((user+‘:‘+pwd).encode(encoding=‘utf-8‘)).decode(encoding=‘utf-8‘) heads = { ‘Accept‘: ‘*/*‘, ‘User-Agent‘: ‘Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET4.0C; .NET4.0E)‘, ‘Referer‘ : ‘http://‘ + ip + ‘/userRpm/StatusRpm.htm‘, #‘Authorization‘ : auth ‘Cookie‘: ‘Authorization=‘ + auth } try: r = requests.get(url=desturl, headers=heads) print(r) except: exMsg = ‘* exMsg:\n‘ + traceback.print_exc() print(exMsg) if __name__ == "__main__": ChangeIP()
【注意】
认证部分不能完全照搬,可通过浏览器分析请求头。
参考:
1、用Python重启TP-Link路由器(重启)
2、Python登录TP-Link路由器换ip脚本(重连)
*** walker ***
本文出自 “walker的流水账” 博客,请务必保留此出处http://walkerqt.blog.51cto.com/1310630/1681948
原文地址:http://walkerqt.blog.51cto.com/1310630/1681948