1.获取登录和注销的请求地址获取数据
如果校园网使用的POST,可以看到data,如果使用的是GET,直接复制请求URL即可。
在抓包的时候记得保留日志
一般来说校园网验证不需要headers模拟,如果使用不了可以尝试一下
def login_param(username, password,isp):
isp_name = ''
if isp == '1':
isp_name = '@zgyd'
elif isp == '2':
isp_name = '@ctc'
elif isp == '3':
isp_name = '@cucc'
return 'c=Portal&a=login&callback=dr1003&login_method=1&'+\
f'user_account=,b,{username}{isp_name}&user_password={password}'+\
'&wlan_user_ip=10.10.48.146&wlan_user_ipv6=&wlan_user_mac=000000000000&wlan_ac_ip=&wlan_ac_name=&jsVersion=3.3.3&v=5241'
def logout_param():
return 'c=Portal&a=logout&callback=dr1004&login_method=1&user_account=drcom'+ \
'&user_password=123&ac_logout=1®ister_mode=1&wlan_user_ip=10.10.48.146'+\
'&wlan_user_ipv6=&wlan_vlan_id=1&wlan_user_mac=000000000000&wlan_ac_ip=&wlan_ac_name=&jsVersion=3.3.3&v=3349'
这样是实现了不同方式的登录和注销
主要取决于校园网的验证系统是否相同
这是我们学校的界面,如果界面类似,
只需要更换
wlan_user_ip=10.10.48.146
这里的ip地址(表示的是你电脑的ip地址,登录网页的下方会显示)请求的的ip地址(校园网验证的的ip地址)
2.所有代码实现
import sys
import re
import json
from getpass import getpass
import requests
if __name__ == '__main__':
def no_traceback(t, e, b):
e = str(e)
s = t.__name__
if e:
s += ': ' + e
print(s)
sys.excepthook = no_traceback
def login_param(username, password,isp):
isp_name = ''
if isp == '1':
isp_name = '@zgyd'
elif isp == '2':
isp_name = '@ctc'
elif isp == '3':
isp_name = '@cucc'
return 'c=Portal&a=login&callback=dr1003&login_method=1&'+\
f'user_account=,b,{username}{isp_name}&user_password={password}'+\
'&wlan_user_ip=10.10.48.146&wlan_user_ipv6=&wlan_user_mac=000000000000&wlan_ac_ip=&wlan_ac_name=&jsVersion=3.3.3&v=5241'
def logout_param():
return 'c=Portal&a=logout&callback=dr1004&login_method=1&user_account=drcom'+ \
'&user_password=123&ac_logout=1®ister_mode=1&wlan_user_ip=10.10.48.146'+\
'&wlan_user_ipv6=&wlan_vlan_id=1&wlan_user_mac=000000000000&wlan_ac_ip=&wlan_ac_name=&jsVersion=3.3.3&v=3349'
def main():
response = requests.get('http://a.suda.edu.cn/')
match = re.search(r'page.run\((.*?)\)', response.text)
if match is None:
sys.exit('登录页面异常')
is_login = match.group(1) == '1'
if not is_login:
try:
print('0:校园网 1:中国移动 2:中国电信 3:中国联通')
isp_sel = input('运营商:')
username = input('账号: ')
password = getpass('密码: ')
except (KeyboardInterrupt, EOFError):
print()
sys.exit()
response = requests.get('http://a.suda.edu.cn:801/eportal/?'+login_param(username, password,isp_sel))
text = response.text
result = json.loads(text[text.find('{'):text.rfind('}') + 1])
if result['result'] == '1':
print('认证成功')
else:
print('用户名或密码错误')
else:
match = re.search(r"uid\s*=\s*\'(.*?)\'", response.text)
if match is None:
uid = 'unknown'
else:
uid = match.group(1)
print('当前登录账号:', uid)
try:
confirm = input('是否注销(y/N)? ').lower() == 'y'
except (KeyboardInterrupt, EOFError):
print()
sys.exit()
if confirm:
response = requests.get('http://10.9.1.3:801/eportal/?'+logout_param())
text = response.text
result = json.loads(text[text.find('{'):text.rfind('}') + 1])
if result['result'] == '1':
print('注销成功')
else:
print('注销失败')
if __name__ == '__main__':
main()
上述文件是实现注销和登录的操作
对于服务器,可以在~/.bashrc中添加命令如
alias login='python login.py'
的方式,可以在bash中使用login登录,方便快捷