-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.py
131 lines (100 loc) · 4.03 KB
/
client.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import select
import socket
import os
import subprocess
import json
import sys
import time
import general
TCP_IP = '192.168.43.154'
TCP_PORT = int(input('Enter Port: '))
BUFFER_SIZE = 10240
TIMEOUT = 2 # for input()
def execute_code(path):
start = time.time()
# code = subprocess.Popen(["python3", path], stdout=subprocess.PIPE)
try:
code = subprocess.check_call(["python3", path], stdout=subprocess.PIPE)
# while code.returncode is None:
# # output = code.stdout.readline()
# # s.send(output)
# code.poll()
end = time.time()
return "{:.3f}".format(end - start)
except subprocess.CalledProcessError:
return "FAIL"
def main():
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((TCP_IP, TCP_PORT))
print("Connected..")
while True:
print("Waiting for server..")
received = s.recv(BUFFER_SIZE)
print(received)
received = received.decode('utf-8')
data = json.loads(received)
if data["type"] == "question":
if data["question"] == "role":
response = dict()
response['type'] = "introduction"
response["host"] = socket.gethostname()
response["role"] = "client"
s.send(json.dumps(response).encode('utf-8'))
elif data["question"] == "input_data":
print("Do you want to continue(y/n): ", end="")
sys.stdout.flush()
choice = 'y'
i, o, e = select.select([sys.stdin], [], [], TIMEOUT)
if (i):
choice = sys.stdin.readline().strip()
else:
choice = 'y'
response = {'type': "response_input"}
if choice == 'n':
response['response'] = 'no'
s.send(json.dumps(response).encode('utf-8'))
print("Closing Connection.....")
s.close()
exit()
else:
response['response'] = 'yes'
s.send(json.dumps(response).encode('utf-8'))
elif data["type"] == "assess":
cwd = os.getcwd()
path = os.path.join(cwd, data['type'])
general.receive_folder(s, path, data, "acknowledge_assess")
elif data["type"] == "actual_code":
cwd = os.getcwd()
path = os.path.join(cwd, 'actual')
general.receive_folder(s, path, data, "acknowledge_actual_code")
elif data["type"] == "actual_input":
cwd = os.getcwd()
path = os.path.join(cwd, 'actual')
general.receive_folder(s, path, data, "acknowledge_actual_input")
code_file_path = os.path.join(path, 'code.py')
output_path = os.path.join(path, 'output')
time.sleep(1)
time_taken = execute_code(code_file_path)
if time_taken == "FAIL":
response = {'type': 'finished', 'status': 'failure'}
s.send(json.dumps(response).encode('utf-8'))
else:
response = {'type': 'finished', 'status': 'success'}
s.send(json.dumps(response).encode('utf-8'))
print("Waiting for server..")
received = s.recv(BUFFER_SIZE)
print(received)
received = received.decode('utf-8')
data = json.loads(received)
if data["type"] == "acknowledge_finished" and time_taken != "FAIL":
general.send_folder(s, output_path, "result", time_taken)
# time.sleep(30)
os.remove(os.path.join(path, 'input.txt'))
# for files in os.listdir(output_path):
# file_path = os.path.join(output_path, files)
# os.remove(file_path)
# os.remove(output_path)
subprocess.Popen(['rm', '-rf', output_path])
elif data["type"] == "error":
print("Error: " + data['error'])
main()