Last active
April 1, 2023 17:16
-
-
Save kirakiseki/44f8283ffec7431300c1191f718a5a79 to your computer and use it in GitHub Desktop.
DHU Portal Auto Login
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from urllib import parse | |
import requests | |
REDIRECT_URL = 'http://baidu.com/?cmd=redirect&arubalp=12345' | |
POST_URL = 'https://portalwy.dhu.edu.cn/post.php' | |
USERNAME = 'USERNAME' | |
PASSWORD = 'PASSWORD' | |
def getSESSID(): | |
global POST_URL | |
resp = requests.session().get(REDIRECT_URL, allow_redirects=True) | |
# set PHPSESSID in /switch.php | |
if not resp.history: | |
print("Already logged in!") | |
return {"status": 0, "cookie": None} | |
else: | |
print(resp.history[2].cookies) | |
param = parse.urlparse(resp.history[2].url) | |
POST_URL = POST_URL.replace("portalwy.dhu.edu.cn", param.netloc) | |
return {"status": 1, "cookie": resp.history[2].cookies} | |
def login(): | |
sessid_resp = getSESSID() | |
if sessid_resp.get("status") == 1: | |
cookies = sessid_resp.get("cookie") | |
print(cookies) | |
print(POST_URL) | |
resp = requests.session().post(POST_URL, cookies=cookies, data={"username": USERNAME, "password": PASSWORD}) | |
if "error" in resp.history[1].url: | |
param = parse.urlparse(resp.history[1].url) | |
qs = parse.parse_qs(param.query) | |
print(f"Login Failed!,Error:{qs.get('error')[0]}") | |
else: | |
print("Login Succeed!") | |
elif sessid_resp.get("status") == 0: | |
print("Skipped login...") | |
if __name__ == '__main__': | |
login() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment