Exercise2
Exercise2
Aim: To Create a Python RPC server to calculate the tax based on the salary input and
return the total tax liability along with the breakdown of tax for each slab for the old tax
regime.
Objective: The objective of the program is to calculate the income tax for a user based
on their annual income and selected tax regime (New or Old) using a client-server
architecture, where the client collects user input and the server processes the tax
calculation and returns the result.
Program:
Server:
import socket
def new_tax_regime(annual_income):
if annual_income <= 300000:
tax = 0
breakdown = [0]
elif annual_income > 300000 and annual_income <= 700000:
tax = 0.05*(annual_income - 300000)
breakdown = [0 , 0.05*(annual_income - 300000) ]
elif annual_income > 700000 and annual_income <= 1000000:
tax = 0.05*(400000) + 0.1*(annual_income - 700000)
breakdown = [0 , 0.05*(400000) , 0.1*(annual_income - 700000)]
elif annual_income > 1000000 and annual_income <= 1200000:
tax = 0.05*(400000) + 0.1*(300000) + 0.15*(annual_income - 1000000)
breakdown = [0 , 0.05*(400000) , 0.1*(300000) , 0.15*(annual_income - 1000000)]
elif annual_income > 1200000 and annual_income <= 1500000:
tax = 0.05*(400000) + 0.1*(300000) + 0.15*(200000) + 0.2*(annual_income - 1200000)
breakdown = [0 , 0.05*(400000) , 0.1*(300000) , 0.15*(200000) , 0.2*(annual_income -
1200000)]
elif annual_income > 1500000:
tax = 0.05*(400000) + 0.1*(300000) + 0.15*(200000) + 0.2*(300000) + 0.3*(annual_income
- 1500000)
breakdown = [0 , 0.05*(400000) , 0.1*(300000) , 0.15*(200000) , 0.2*(300000) ,
0.3*(annual_income - 1500000)]
else:
print("Invalid value")
tax = None
breakdown = None
return tax, breakdown
def old_tax_regime(annual_income):
if annual_income <= 250000:
tax = 0
breakdown = [0]
elif annual_income > 250000 and annual_income <= 300000:
tax = 0.05*(annual_income - 250000)
breakdown = [0 , 0.05*(annual_income - 250000) ]
elif annual_income > 300000 and annual_income <= 500000:
tax = 0.05*(50000) + 0.5*(annual_income - 300000)
breakdown = [0 , 0.05*(50000), 0.5*(annual_income - 300000)]
elif annual_income > 500000 and annual_income <= 1000000:
tax = 0.05*(50000) + 0.05*(200000) + 0.2*(annual_income - 500000)
breakdown = [0 , 0.05*(50000) , 0.05*(200000) , 0.2*(annual_income - 500000) ]
elif annual_income > 1000000:
tax = 0.05*(50000) + 0.05*(200000) + 0.2*(500000) + 0.3*(annual_income - 1000000)
breakdown = [0 , 0.05*(50000) , 0.05*(200000) , 0.2*(500000) , 0.3*(annual_income -
1000000)]
else:
print("Invalid value")
tax = None
breakdown = None
return tax, breakdown
def handle_client(client_socket):
# Receive data from client
data = client_socket.recv(1024).decode()
income, regime = data.split(',')
annual_income = float(income)
regime = regime.strip().lower()
if regime == "new":
tax, breakdown = new_tax_regime(annual_income)
response = f"New Tax Regime: Tax = {tax}, Breakdown = {breakdown}"
elif regime == "old":
tax, breakdown = old_tax_regime(annual_income)
response = f"Old Tax Regime: Tax = {tax}, Breakdown = {breakdown}"
else:
response = "Invalid Regime Selected"
client_socket.send(response.encode())
client_socket.close()
def main():
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(("0.0.0.0", 12345))
server.listen(5)
print("Server listening on port 12345")
while True:
client_socket, addr = server.accept()
print(f"Connection received from {addr}")
handle_client(client_socket)
if __name__ == "__main__":
main()
Client:
import socket
def main():
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect(("127.0.0.1", 12345))
if __name__ == "__main__":
main()
Explanation of the Program:
Server side:
• The server listens for incoming client connections on a specific port.
• Accepts the client's request and receives the income and regime choice data.
• Processes the data to calculate tax based on the chosen tax regime (New or Old).
• Sends the calculated tax and breakdown back to the client as a response.
• Closes the connection with the client after sending the response.
Client side:
• The client collects the user's annual income and choice of tax regime (New or Old).
• Establishes a socket connection to communicate with the server.
• Sends the entered income and regime choice to the server.
• Waits for the server's response containing the tax details.
• Displays the received tax and breakdown to the user and closes the connection.
Output:
Server:
Client:
Conclusion: Therefore, the client sends annual income and tax regime choice to the
server for processing. The server calculates the tax based on the regime and sends the
result back to the client.