Script Functions and Code Analysis
1. Script Overview
This Python script aims to interact with the API via network requests to achieve functions such as obtaining user tokens (Token), querying the number of unattended sessions, obtaining attendance codes, and completing attendance. It is mainly targeted at the "Shangkele" (Class Is On) digital attendance system, interacting with the system by simulating network requests.
2. Code Detailed Explanation
2.1 Fetch Token: fetch_token(uid)
- Function: Sends a POST request to a specific URL to obtain the user's Token.
- Parameters:
uid- The user's ID. - Returns: If the request is successful (status code 200), returns the Token; otherwise returns None.
- Implementation Details:
- Uses
requests.postto send a POST request to the URL. - Includes customized headers to simulate browser access.
- Parses the response content as JSON and checks the status code and Token value.
- Uses
2.2 Get Unattended Count: get_uncall_number(token)
- Function: Uses the Token to query the current number of unattended sessions.
- Parameters:
token- The Token used for authentication. - Implementation Details:
- Sends a GET request carrying the Token in the header.
- Parses the response content and processes it according to the status code and returned data.
2.3 Get Attendance Code: create_call_number(token, call_id)
- Function: Obtains the attendance code.
- Parameters:
token- The Token used for verification.call_id- The attendance ID.
- Returns: Attendance code or None (if failed).
- Implementation Details:
- Sends a POST request containing the Token and attendance ID.
- Parses the response to obtain the attendance code.
2.4 Complete Attendance: validate_call_number(token, call_stu_id, num)
- Function: Sends the given attendance code to complete the attendance.
- Parameters:
token- Token value.call_stu_id- Student attendance ID.num- Attendance code.
- Implementation Details:
- Sends a POST request containing necessary verification information.
- Parses the response and outputs it.
2.5 Main Function: main()
- Function: Reads the user ID list, obtains a Token for each user, and executes subsequent operations.
- Implementation Details:
- Reads user IDs from the
uid.txtfile. - For each ID, obtains a Token and performs subsequent operations.
- Reads user IDs from the
2.6 Entry Point
- Code:
if __name__ == "__main__": main() - Function: Ensures that the main function is called when the script is run as the main program.
3. Code Usage
- Preparation: A text file containing user IDs (uid.txt).
- Run: Execute the script using a Python environment.
- Output: Outputs different results based on the script logic, such as Token information, attendance codes, validation results, etc.
Note: This script involves network requests and data processing and should be used in compliance with relevant laws, regulations, and cybersecurity guidelines.
4. Specific Code
import requests
def fetch_token(uid):
"""
Sends a POST request to obtain the token value.
Parameters:
uid: The user's ID.
Returns:
If status code is 200, returns the token value, otherwise returns None.
"""
url = "http://*****.cn/api/Main/Quick"
headers = {
"Connection": "keep-alive",
"Accept": "application/json, text/javascript, */*; q=0.01",
"DNT": "1",
"X-Requested-With": "XMLHttpRequest",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36 Edg/122.0.0.0",
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
"Origin": "http://******",
"Referer": f"http://***************?uid={uid}",
"Accept-Encoding": "gzip, deflate",
"Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
}
data = {"uid": uid}
response = requests.post(url, headers=headers, data=data)
try:
response_json = response.json()
if response_json.get("state") == 200:
return response_json.get("token")
except ValueError:
print("Response content cannot be parsed as JSON.")
return None
def get_uncall_number(token):
"""
Uses token to send a GET request to obtain the number of uncalled sessions.
Parameters:
token: The token value used for verification.
Returns:
The text content of the response.
"""
url = "http://********.cn/api/Call/GetUnCallNumber"
headers = {
"Connection": "keep-alive",
"Accept": "application/json, text/javascript, */*; q=0.01",
"DNT": "1",
"X-Requested-With": "XMLHttpRequest",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36 Edg/122.0.0.0",
"Auth": token,
"Origin": "http://******",
"Referer": "http://********/wei/pages/attendance/call_sign.html?1711430518796",
"Accept-Encoding": "gzip, deflate",
"Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
}
response = requests.post(url, headers=headers)
response_json = response.json()
# Process response
if response_json.get("state") == "ok" and "msg" in response_json:
print("No digital attendance pending sign-in")
elif response_json.get("state") == "ok" and "callStuId" in response_json:
call_id = response_json["call"]["CallId"]
call_stu_id = response_json["callStuId"] # Get callStuId
course_name = response_json["call"]["CourseName"] # Get course name
print("Course Name:", course_name) # Print course name
num = create_call_number(token, call_id) # Get attendance code
if num:
validate_call_number(token, call_stu_id, num) # Validate attendance code
def create_call_number(token, call_id):
"""
Uses token and call_id to send a POST request to obtain the attendance code.
Parameters:
token: The token value used for verification.
call_id: Attendance ID.
Returns:
Attendance code.
"""
url = "http://******/api/Call/CreateCallNumber"
headers = {
"Connection": "keep-alive",
"Accept": "application/json, text/javascript, */*; q=0.01",
"DNT": "1",
"X-Requested-With": "XMLHttpRequest",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36 Edg/122.0.0.0",
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
"Auth": token,
"Origin": "http://******",
"Referer": "http://******/wei/pages/attendance/call_num.html?callid=" + call_id,
"Accept-Encoding": "gzip, deflate",
"Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
}
data = {
"Flag": "0",
"CallId": call_id
}
response = requests.post(url, headers=headers, data=data)
try:
response_json = response.json()
if response_json.get("state") == "ok" and "data" in response_json:
num = response_json["data"]
print("Attendance Code:", num)
return num
else:
print("Failed to get attendance code")
except ValueError:
print("Response content cannot be parsed as JSON.")
return None
def validate_call_number(token, call_stu_id, num):
"""
Uses token, callStuId, and attendance code num to send a POST request to validate the attendance code.
Parameters:
token: The token value used for verification.
call_stu_id: Student attendance ID.
num: Attendance code.
"""
url = "http://******/api/Call/VaildCallNumber"
headers = {
"Connection": "keep-alive",
"Accept": "application/json, text/javascript, */*; q=0.01",
"DNT": "1",
"X-Requested-With": "XMLHttpRequest",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36 Edg/122.0.0.0",
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
"Auth": token,
"Origin": "http://******",
"Referer": "http://******/wei/pages/attendance/call_sign.html?1711431873822",
"Accept-Encoding": "gzip, deflate",
"Accept-Language": "zh-CN,zh;q=0.9",
}
data = {
"callStuId": call_stu_id,
"num": num
}
response = requests.post(url, headers=headers, data=data)
try:
response_json = response.json()
print("Validate attendance code response:", response_json)
except ValueError:
print("Response content cannot be parsed as JSON.")
def main():
with open('uid.txt', 'r') as file:
uids = file.readlines()
for uid_str in uids:
uid = uid_str.strip() # Remove trailing newline
token = fetch_token(uid)
if token:
print(f"UID {uid} Token fetched successfully:", token)
# Use the fetched token to continue execution logic
get_uncall_number(token) # Please ensure this function and others it calls can handle the token correctly
else:
print(f"UID {uid} Unable to fetch token or status code is not 200.")
# Call main function
if __name__ == "__main__":
main()