Python3控制路由器——使用requests重啟極路由.py

來源:互聯網
上載者:User
通過本文給大家介紹Python3控制路由器——使用requests重啟極路由.py的相關知識,代碼寫了相應的注釋,以後再寫成可以方便調用的模組。

用fiddler抓包可以看到很多HTTP頭,經過嘗試發現不是都必須的。

'Upgrade-Insecure-Requests':1,#必要項,值為1

'Content-Type':'application/x-www-form-urlencoded',#必要項

否則取不到服務順響應返回的Set-Cookie

"""python3控制路由器--使用requests重啟極路由.py2016年5月10日 06:20:56 codegay參考資料requests文檔:http://cn.python-requests.org/zh_CN/latest/"""import requestsimport reurl="http://192.168.199.1/cgi-bin/turbo/admin_web"#用fiddler抓包可以看到很多HTTP頭,經過嘗試發現不是都必須的。#'Upgrade-Insecure-Requests':1,#必要項,值為1#'Content-Type':'application/x-www-form-urlencoded',#必要項#否則取不到服務順響應返回的Set-Cookiehead={#'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8','Upgrade-Insecure-Requests':1,'Content-Type':'application/x-www-form-urlencoded',#'User-Agent':'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.94 Safari/537.36',#'DNT':1,#'Referer':'http://192.168.199.1/cgi-bin/turbo/admin_web'}s=requests.Session()r=s.post(url,data="username=admin&password=你的密碼",headers=head)txt=r.textstok=re.findall('''stok=(\w+).+?reboot''',txt)[0]#stok會得到類似的字串#stok='1f7a2b7034c67401a20d4ce0cdde7c7d'print(stok)rooturl='http://192.168.199.1/cgi-bin/turbo/'stokurl=rooturl + ';stok=%s/api/system/reboot' % stok#帶著成功登入後的cookies,並且找出stok,拼成下如URL get請求一次就可以完成路由的重啟#stokurl='http://192.168.199.1/cgi-bin/turbo/;stok=78e3516718ff32250fa796ed4462188c/api/system/reboot'reboot=s.get(stokurl) #重啟

關於本文給大家介紹的Python3控制路由器——使用requests重啟極路由.py的知識就給大家介紹這麼多,本文代碼內容附有注釋,大家在參考過程中有任何疑問歡迎給我留言,小編會及時回複大家的,在此也非常感謝大家對指令碼之家網站的支援!

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.