-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathudp-server.py
41 lines (29 loc) · 998 Bytes
/
udp-server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import socket
import operator
# 'Converts' the string operator into a real operator
operators = {
"+": operator.add,
"-": operator.sub, '*' : operator.mul,
'/' : operator.truediv,
'%' : operator.mod,
'^' : operator.xor,
}
IP = "127.0.0.1"
PORT = 12000
socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
socket.bind((IP, PORT))
print('Server Listening At {}'.format(socket.getsockname()))
while True:
messageBytes, address = socket.recvfrom(2048)
messageString = messageBytes.decode('utf-8')
print('Received from client {} : {}'.format(address, messageString))
messageString = messageString.split(", ")
a = messageString[0]
b = messageString[1]
operator = messageString[2]
if a == 'X' or b == 'X' or operator == 'X':
break
result = operators[operator](int(a), int(b))
socket.sendto(str(result).encode(), address)
print('Connection Closed')
socket.close()