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
CodeBlock Loading...