Board logo

标题: [原创代码] python3控制路由器--使用requests重启极路由.py [打印本页]

作者: codegay    时间: 2016-5-10 09:45     标题: python3控制路由器--使用requests重启极路由.py

python3控制路由器--使用requests重启极路由.py
  1. """
  2. python3控制路由器--使用requests重启极路由.py
  3. 2016年5月10日 06:20:56 codegay
  4. 参考资料requests文档:
  5. http://cn.python-requests.org/zh_CN/latest/
  6. """
  7. import requests
  8. import re
  9. url="http://192.168.199.1/cgi-bin/turbo/admin_web"
  10. #用fiddler抓包可以看到很多HTTP头,经过尝试发现不是都必须的。
  11. #'Upgrade-Insecure-Requests':1,#必要项,值为1
  12. #'Content-Type':'application/x-www-form-urlencoded',#必要项
  13. #否则取不到服务顺响应返回的Set-Cookie
  14. head={#'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
  15.       'Upgrade-Insecure-Requests':1,
  16.       'Content-Type':'application/x-www-form-urlencoded',
  17.       #'User-Agent':'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.94 Safari/537.36',
  18.       #'DNT':1,
  19.       #'Referer':'http://192.168.199.1/cgi-bin/turbo/admin_web'
  20.       }
  21. s=requests.Session()
  22. r=s.post(url,data="username=admin&password=你的密码",headers=head)
  23. txt=r.text
  24. stok=re.findall('''stok=(\w+).+?reboot''',txt)[0]
  25.     #stok会得到类似的字符串
  26.     #stok='1f7a2b7034c67401a20d4ce0cdde7c7d'
  27. print(stok)
  28. rooturl='http://192.168.199.1/cgi-bin/turbo/'
  29. stokurl=rooturl + ';stok=%s/api/system/reboot' % stok
  30. #带着成功登录后的cookies,并且找出stok,拼成下如URL get请求一次就可以完成路由的重启
  31. #stokurl='http://192.168.199.1/cgi-bin/turbo/;stok=78e3516718ff32250fa796ed4462188c/api/system/reboot'
  32. reboot=s.get(stokurl) #重启
复制代码

作者: happy886rr    时间: 2016-5-10 09:51

回复 1# codegay
从你的py和julia代码中学到很多技巧。
作者: codegay    时间: 2016-5-10 09:55

回复 2# happy886rr


    哈哈。互相学习。你多写算法题。




欢迎光临 批处理之家 (http://www.bathome.net/) Powered by Discuz! 7.2