0% found this document useful (0 votes)
17 views2 pages

Socket: Import

This Python code defines a basic client-server application for performing mathematical operations. The server code creates a socket, binds it to a port, listens for client connections, receives operation strings, performs the calculation, and returns the result to the client. The client code connects to the server socket, prompts the user for an operation, sends the operation string, receives the result, and prints it. When the client inputs "Over", the connection closes.

Uploaded by

oobaa bbbssa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views2 pages

Socket: Import

This Python code defines a basic client-server application for performing mathematical operations. The server code creates a socket, binds it to a port, listens for client connections, receives operation strings, performs the calculation, and returns the result to the client. The client code connects to the server socket, prompts the user for an operation, sends the operation string, receives the result, and prints it. When the client inputs "Over", the connection closes.

Uploaded by

oobaa bbbssa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

#server

import socket
LOCALHOST = "127.0.0.1"
PORT = 8080
server = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
server.bind((LOCALHOST, PORT))
server.listen(1)
print("Server started")
print("Waiting for client request..")

clientConnection, clientAddress = server.accept()


print("Connected client :", clientAddress)
msg = ''
while True:
data = clientConnection.recv(1024)
msg = data.decode()
if msg == 'Over':
print("Connection is Over")
break
print("Equation is received")
result = 0
operation_list = msg.split()
oprnd1 = operation_list[0]
operation = operation_list[1]
oprnd2 = operation_list[2]
num1 = int(oprnd1)
num2 = int(oprnd2)
if operation == "+":
result = num1 + num2
elif operation == "-":
result = num1 - num2
elif operation == "/":
result = num1 / num2
elif operation == "*":
result = num1 * num2
print("Send the result to client")
output = str(result)
clientConnection.send(output.encode())
clientConnection.close()

#Client
import socket
SERVER = "127.0.0.1"
PORT = 8080
client = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
client.connect((SERVER, PORT))
while True:
print("Example : 4 + 5")
inp = input("Enter the operation in \the form opreand operator oprenad: ")
if inp == "Over":break
client.send(inp.encode())
answer = client.recv(1024)
print("Answer is "+answer.decode())
print("Type 'Over' to terminate")

client.close()

You might also like