您的位置:首页 > 编程语言 > Python开发

Python3切换TP-LINK外网IP(ADSL)

2015-08-05 16:07 656 查看
【代码】
#encoding=utf-8
#author:walker
#date:2015-08-05
#summary:通过断开连接时TP-LINK重新拨号,以切换IP。此代码经过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脚本(重连)
3、Python一键设置路由器

*** walker ***
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息