dsfggg 发表于 2015-8-6 10:49:56

Python3切换TP-LINK外网IP

【代码】

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
#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脚本(重连)
3、Python一键设置路由器

*** walker ***


页: [1]
查看完整版本: Python3切换TP-LINK外网IP