背景
在校園里認證上網很麻煩需要web輸入賬號密碼有時還會忘記web地址此時就需要一個人或者程序幫我們實現,這時我想到用python制作這個程序(初學者python代碼不規范)
分析
需要分析web登錄網址的瀏覽器頭發現是get方法這就簡單了,再次分析get請求發現有user_account字段,user_password字段還有ip字段mac字段這時我們的思路就來了使用curl命令直接把這個代碼放到終端里運行發現是可以的
1
|
curl "http://學校認證服務器ip:801/eportal/?c=Portal&a=login&callback=dr1004&login_method=1&user_account=你的賬號&user_password=你的密碼&wlan_user_ip=終端的ip&wlan_user_ipv6=&wlan_user_mac=終端的mac&wlan_ac_ip=&wlan_ac_name=&jsVersion=3.3.3&v=隨機四位數/" |
返回信息
1
|
dr1004({ "result" : "1" , "msg" : "\u8ba4\u8bc1\u6210\u529f" }) |
使用unicode在線轉中文發現
1
|
dr1004({result: "1" , msg: "認證成功" }) |
理論成功
實現
經過分析我們需要以下信息
1.上網賬號
2.賬號密碼
3.設備ip
4.設備mac
5.4位隨機數
獲取ip(wlan連接)
網上方法很多但都獲取不到正確的索性用最笨的方法獲取調用 ipconfig /all 方法
1
2
3
4
5
6
|
import random import os import requests from urllib import parse mac_ip_hostname = os.popen( "ipconfig /all" ) macmore = mac_ip_hostname.read() |
此時獲取的是一大堆網絡信息并不是我們想要的所以要用到find方法找到特殊字段的位置
1
|
macw = macmore.find( "無線局域網適配器 WLAN" ) |
找到了不代表能用現在需要把這個字符串進行截取從找到的位置到之后的400個字符的數據都截取下來
再賦值給別的函數
1
2
3
4
5
6
|
macm = '' i = macw y = macw + 400 while i< y: macm + = macmore[i] i = i + 1 |
這時定義一個函數來接收400個字符數據,現在的問題是我們還是不能直接使用還要進行截取我們需要的數據,我們還需要find找到 物理地址 這個字段的數據,如法炮制我們進行3次查找就找到了所需要的數據(如果是lan 網線的話,方法一樣)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
macxw = macm.find( "物理地址" ) #print(macxw) x = macxw y = macxw + 50 macxm = '' while x<y: macxm + = macm[x] x = x + 1 maci = macxm.find( "-" ) m1 = maci - 2 m2 = maci - 1 m3 = maci + 1 m4 = maci + 2 m5 = maci + 4 m6 = maci + 5 m7 = maci + 7 m8 = maci + 8 m9 = maci + 10 m10 = maci + 11 m11 = maci + 13 m12 = maci + 14 mac = (macxm[m1] + macxm[m2] + macxm[m3] + macxm[m4] + macxm[m5] + macxm[m6] + macxm[m7] + macxm[m8] + macxm[m9] + macxm[m10] + macxm[m11] + macxm[m12]) |
我們獲取到了mac 這時我們還缺少ip數據,像ip這種數據不是固定字符所以不能用上述一個方法來取值還需要rindex方法,先用上面的方法進行截取數據到IP的數據時我們先將字符串里的數字先遍歷出來找到第一個數字的位置,和最后一個數字的位置然后我們在取值就完美解決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
|
ipcxw = macm.find( "IPv4 地址" ) o = ipcxw + 10 p = ipcxw + 70 ipc = '' while o<p: ipc + = macm[o] o = o + 1 #print(ipc) op = 0 ipsd = '' is_op = '0123456789' isstart = False #定義是否是數字開始的標記變量 for a in ipc: #將數字循環遍歷 if a in is_op: #判斷取出來的數字是否是數字 if isstart = = True : ipsd = ipsd + a else : ipsd = ipsd + a isstart = True ipzh = (ipsd[ len (ipsd) - 1 ]) ipks = (ipsd[ 0 ]) ipce = ipc.find(ipks) ipcea = ipc.rindex(ipzh) j = ipce l = ipcea + 1 ip = '' while j<l: ip + = ipc[j] j = j + 1 |
我們還需要隨機四位數
1
2
3
4
|
c = '' for i in range ( 4 ): ch = chr (random.randrange( ord ( '0' ), ord ( '9' ) + 1 )) c + = ch |
現在我們要用到requests庫中的get方法來進行與服務器交流
1
2
|
url = "http://學校認證服務器ip:801/eportal/?c=Portal&a=login&callback=dr1004&login_method=1&user_account=" + user + "&user_password=" + password + "&wlan_user_ip=" + ip + "&wlan_user_ipv6=&wlan_user_mac=" + mac + "&wlan_ac_ip=&wlan_ac_name=&jsVersion=3.3.3&v=" + c + "" (user = 你的用戶,password = 你的賬戶密碼) qinqiu = requests.get(url) |
現在我們需要服務器給我們返回信息
1
|
print ( str (qinqiu.content))<font face = "Arial, Verdana, sans-serif" ><span style = "white-space: normal;" > < / span>< / font> |
到此這篇關于Python實現連接dr校園網示例詳解的文章就介紹到這了,更多相關Python連接校園網內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!
原文鏈接:https://blog.csdn.net/v225m/article/details/121592005