0% found this document useful (0 votes)
1 views

server.py

This document contains a Python script for a CGPA calculator server that uses sockets to handle client requests. It defines a grading schema, calculates grades and GPAs based on student inputs, and logs the results to a file. The server listens for connections and processes multiple clients concurrently using threading.

Uploaded by

Ayesha A. Rahman
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

server.py

This document contains a Python script for a CGPA calculator server that uses sockets to handle client requests. It defines a grading schema, calculates grades and GPAs based on student inputs, and logs the results to a file. The server listens for connections and processes multiple clients concurrently using threading.

Uploaded by

Ayesha A. Rahman
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

import socket

import threading

# Grading schema based on the provided table


GRADING_SCHEMA = [
(90, "A+", 4.00), (86, "A", 4.00), (82, "A-", 3.67),
(78, "B+", 3.33), (74, "B", 3.00), (70, "B-", 2.67),
(66, "C+", 2.33), (62, "C", 2.00), (58, "C-", 1.67),
(54, "D+", 1.33), (50, "D", 1.00)
]

# Function to determine grade and GPA


def get_grade_and_gpa(marks):
for threshold, grade, gpa in GRADING_SCHEMA:
if marks >= threshold:
return grade, gpa
return "F", 0.00 # Below 50 is considered Fail

# Function to handle client requests


def handle_client(client_socket):
client_socket.send(b"Welcome to FAST-NUCES Karachi Campus CGPA Calculator!\n")

# Get student ID
client_socket.send(b"Enter your student ID: ")
student_id = client_socket.recv(1024).decode().strip()

# Get number of subjects


client_socket.send(b"Enter number of subjects: ")
num_subjects = int(client_socket.recv(1024).decode().strip())

total_credit_hours = 0
total_weighted_gpa = 0
results = []

for i in range(num_subjects):
client_socket.send(f"Enter credit hours for subject {i+1}: ".encode())
credit_hours = int(client_socket.recv(1024).decode().strip())

client_socket.send(f"Enter marks obtained for subject {i+1} (out of 100):


".encode())
marks = int(client_socket.recv(1024).decode().strip())

grade, gpa = get_grade_and_gpa(marks)

total_credit_hours += credit_hours
total_weighted_gpa += gpa * credit_hours
results.append((marks, credit_hours, grade, gpa))

# Calculate CGPA
cgpa = total_weighted_gpa / total_credit_hours if total_credit_hours > 0 else 0

# Send results to client


response = "\nYour CGPA Report:\n"
for i, (marks, credit_hours, grade, gpa) in enumerate(results, 1):
response += f"Subject {i}: Marks = {marks}, Credit Hours = {credit_hours},
Grade = {grade}, GPA = {gpa:.2f}\n"
response += f"\nOverall CGPA: {cgpa:.2f}\n"

client_socket.send(response.encode())
# Log data to file
with open("cgpa_log.txt", "a") as log_file:
log_file.write(f"ID: {student_id}, Credit Hours: {total_credit_hours},
CGPA: {cgpa:.2f}\n")
for i, (marks, credit_hours, grade, gpa) in enumerate(results, 1):
log_file.write(f"Subject {i}: Marks = {marks}, Credit Hours =
{credit_hours}, Grade = {grade}, GPA = {gpa:.2f}\n")
log_file.write("--------------------------------------------------\n")

client_socket.close()

# Main server function


def start_server():
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(("0.0.0.0", 12345))
server.listen(5)
print("Server is listening on port 12345...")

while True:
client_socket, addr = server.accept()
print(f"Accepted connection from {addr}")
client_handler = threading.Thread(target=handle_client,
args=(client_socket,))
client_handler.start()

if __name__ == "__main__":
start_server()

You might also like