背景
在校園里認(rèn)證上網(wǎng)很麻煩需要web輸入賬號(hào)密碼有時(shí)還會(huì)忘記web地址此時(shí)就需要一個(gè)人或者程序幫我們實(shí)現(xiàn),這時(shí)我想到用python制作這個(gè)程序(初學(xué)者python代碼不規(guī)范)
分析
需要分析web登錄網(wǎng)址的瀏覽器頭發(fā)現(xiàn)是get方法這就簡(jiǎn)單了,再次分析get請(qǐng)求發(fā)現(xiàn)有user_account字段,user_password字段還有ip字段mac字段這時(shí)我們的思路就來(lái)了使用curl命令直接把這個(gè)代碼放到終端里運(yùn)行發(fā)現(xiàn)是可以的
1
|
curl "http://學(xué)校認(rèn)證服務(wù)器ip:801/eportal/?c=Portal&a=login&callback=dr1004&login_method=1&user_account=你的賬號(hào)&user_password=你的密碼&wlan_user_ip=終端的ip&wlan_user_ipv6=&wlan_user_mac=終端的mac&wlan_ac_ip=&wlan_ac_name=&jsVersion=3.3.3&v=隨機(jī)四位數(shù)/" |
返回信息
1
|
dr1004({ "result" : "1" , "msg" : "\u8ba4\u8bc1\u6210\u529f" }) |
使用unicode在線轉(zhuǎn)中文發(fā)現(xiàn)
1
|
dr1004({result: "1" , msg: "認(rèn)證成功" }) |
理論成功
實(shí)現(xiàn)
經(jīng)過(guò)分析我們需要以下信息
1.上網(wǎng)賬號(hào)
2.賬號(hào)密碼
3.設(shè)備ip
4.設(shè)備mac
5.4位隨機(jī)數(shù)
獲取ip(wlan連接)
網(wǎng)上方法很多但都獲取不到正確的索性用最笨的方法獲取調(diào)用 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() |
此時(shí)獲取的是一大堆網(wǎng)絡(luò)信息并不是我們想要的所以要用到find方法找到特殊字段的位置
1
|
macw = macmore.find( "無(wú)線局域網(wǎng)適配器 WLAN" ) |
找到了不代表能用現(xiàn)在需要把這個(gè)字符串進(jìn)行截取從找到的位置到之后的400個(gè)字符的數(shù)據(jù)都截取下來(lái)
再賦值給別的函數(shù)
1
2
3
4
5
6
|
macm = '' i = macw y = macw + 400 while i< y: macm + = macmore[i] i = i + 1 |
這時(shí)定義一個(gè)函數(shù)來(lái)接收400個(gè)字符數(shù)據(jù),現(xiàn)在的問(wèn)題是我們還是不能直接使用還要進(jìn)行截取我們需要的數(shù)據(jù),我們還需要find找到 物理地址 這個(gè)字段的數(shù)據(jù),如法炮制我們進(jìn)行3次查找就找到了所需要的數(shù)據(jù)(如果是lan 網(wǎng)線的話,方法一樣)
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 這時(shí)我們還缺少ip數(shù)據(jù),像ip這種數(shù)據(jù)不是固定字符所以不能用上述一個(gè)方法來(lái)取值還需要rindex方法,先用上面的方法進(jìn)行截取數(shù)據(jù)到IP的數(shù)據(jù)時(shí)我們先將字符串里的數(shù)字先遍歷出來(lái)找到第一個(gè)數(shù)字的位置,和最后一個(gè)數(shù)字的位置然后我們?cè)谌≈稻屯昝澜鉀Qip地址字符不確定的問(wèn)題
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 #定義是否是數(shù)字開(kāi)始的標(biāo)記變量 for a in ipc: #將數(shù)字循環(huán)遍歷 if a in is_op: #判斷取出來(lái)的數(shù)字是否是數(shù)字 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 |
我們還需要隨機(jī)四位數(shù)
1
2
3
4
|
c = '' for i in range ( 4 ): ch = chr (random.randrange( ord ( '0' ), ord ( '9' ) + 1 )) c + = ch |
現(xiàn)在我們要用到requests庫(kù)中的get方法來(lái)進(jìn)行與服務(wù)器交流
1
2
|
url = "http://學(xué)校認(rèn)證服務(wù)器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) |
現(xiàn)在我們需要服務(wù)器給我們返回信息
1
|
print ( str (qinqiu.content))<font face = "Arial, Verdana, sans-serif" ><span style = "white-space: normal;" > < / span>< / font> |
到此這篇關(guān)于Python實(shí)現(xiàn)連接dr校園網(wǎng)示例詳解的文章就介紹到這了,更多相關(guān)Python連接校園網(wǎng)內(nèi)容請(qǐng)搜索服務(wù)器之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持服務(wù)器之家!
原文鏈接:https://blog.csdn.net/v225m/article/details/121592005