-
Notifications
You must be signed in to change notification settings - Fork 0
/
Server.py
56 lines (43 loc) · 1.48 KB
/
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import argparse
import RDT
import time
def makePigLatin(word):
m = len(word)
vowels = "a", "e", "i", "o", "u", "y"
if m<3 or word=="the":
return word
else:
for i in vowels:
if word.find(i) < m and word.find(i) != -1:
m = word.find(i)
if m==0:
return word+"way"
else:
return word[m:]+word[:m]+"ay"
def piglatinize(message):
essagemay = ""
message = message.strip(".")
for word in message.split(' '):
essagemay += " "+makePigLatin(word)
return essagemay.strip()+"."
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Pig Latin conversion server.')
parser.add_argument('port', help='Port.', type=int)
args = parser.parse_args()
timeout = 5 #close connection if no new data within 5 seconds
time_of_last_data = time.time()
rdt = RDT.RDT('server', None, args.port)
while(True):
#try to receiver message before timeout
msg_S = rdt.rdt_1_0_receive()
if msg_S is None:
if time_of_last_data + timeout < time.time():
break
else:
continue
time_of_last_data = time.time()
#convert and reply
rep_msg_S = piglatinize(msg_S)
print('Converted %s \nto %s\n' % (msg_S, rep_msg_S))
rdt.rdt_1_0_send(rep_msg_S)
rdt.disconnect()