-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
229 lines (163 loc) · 6.87 KB
/
app.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
from flask import Flask, render_template, request, jsonify
import subprocess
from flask_socketio import SocketIO, join_room, leave_room, emit, os
app = Flask(__name__)
app.config['SECRET_KEY'] = os.urandom(5)
socketio = SocketIO(app)
# A set to store active users
active_users = set()
@app.route('/')
def index():
return render_template('index.html')
@app.route('/runcode', methods=['POST'])
def run_code():
code = request.json.get('code', '')
input_data = request.json.get('input', None)
try:
output = compile_and_execute(code, input_data)
return jsonify({'output': output})
except Exception as e:
return jsonify({'error': str(e)})
def compile_and_execute(code, input_data):
temp_file_path = '/tmp/temp_code.py'
with open(temp_file_path, 'w') as temp_file:
temp_file.write(code)
if input_data:
input_file_path = '/tmp/input_data.txt'
with open(input_file_path, 'w') as input_file:
input_file.write(input_data)
command = f'python3 {temp_file_path} < {input_file_path}'
else:
command = f'python3 {temp_file_path}'
process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
output, error = process.communicate()
if error:
raise Exception(f"An error occurred: {error.decode('utf-8')}")
return output.decode('utf-8')
@app.route('/c')
def c():
return render_template('c.html')
@app.route('/runc', methods=['POST'])
def run_c_code():
code = request.json.get('code', '')
input_data = request.json.get('input', None)
try:
output = compile_and_execute_c(code, input_data)
return jsonify({'output': output})
except Exception as e:
return jsonify({'error': str(e)})
def compile_and_execute_c(code, input_data):
language = 'gcc'
extension = 'c'
compiled_file_path = '/tmp/temp_code'
with open(f'{compiled_file_path}.{extension}', 'w') as temp_file:
temp_file.write(code)
command_compile = f'{language} {compiled_file_path}.{extension} -o {compiled_file_path}'
process_compile = subprocess.Popen(command_compile, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
_, error_compile = process_compile.communicate()
if error_compile:
raise Exception(f"Code compilation error: {error_compile.decode('utf-8')}")
if input_data:
input_file_path = '/tmp/input_data.txt'
with open(input_file_path, 'w') as input_file:
input_file.write(input_data)
command_execute = f'{compiled_file_path} < {input_file_path}'
else:
command_execute = compiled_file_path
process_execute = subprocess.Popen(command_execute, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
output, error_execute = process_execute.communicate()
if error_execute:
raise Exception(f"An error occurred during code execution: {error_execute.decode('utf-8')}")
return output.decode('utf-8')
@app.route('/cpp')
def cpp():
return render_template('cpp.html')
@app.route('/runcpp', methods=['POST'])
def run_cpp_code():
code = request.json.get('code', '')
input_data = request.json.get('input', None)
try:
output = compile_and_execute_cpp(code, input_data)
return jsonify({'output': output})
except Exception as e:
return jsonify({'error': str(e)})
def compile_and_execute_cpp(code, input_data):
language = 'g++'
extension = 'cpp'
compiled_file_path = '/tmp/temp_code'
with open(f'{compiled_file_path}.{extension}', 'w') as temp_file:
temp_file.write(code)
command_compile = f'{language} {compiled_file_path}.{extension} -o {compiled_file_path}'
process_compile = subprocess.Popen(command_compile, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
_, error_compile = process_compile.communicate()
if error_compile:
raise Exception(f"Code compilation error: {error_compile.decode('utf-8')}")
if input_data:
input_file_path = '/tmp/input_data.txt'
with open(input_file_path, 'w') as input_file:
input_file.write(input_data)
command_execute = f'{compiled_file_path} < {input_file_path}'
else:
command_execute = compiled_file_path
process_execute = subprocess.Popen(command_execute, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
output, error_execute = process_execute.communicate()
if error_execute:
raise Exception(f"An error occurred during code execution: {error_execute.decode('utf-8')}")
return output.decode('utf-8')
@app.route('/java')
def java():
return render_template('java.html')
@app.route('/runjava', methods=['POST'])
def run_java_code():
code = request.json.get('code', '')
input_data = request.json.get('input', None)
try:
output = compile_and_execute_java(code, input_data)
return jsonify({'output': output})
except Exception as e:
return jsonify({'error': str(e)})
def compile_and_execute_java(code, input_data):
temp_file_path = '/tmp/temp_code.java'
with open(temp_file_path, 'w') as temp_file:
temp_file.write(code)
compiled_file_path = '/tmp'
command_compile = f'javac {temp_file_path} -d {compiled_file_path}'
process_compile = subprocess.Popen(command_compile, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
_, error_compile = process_compile.communicate()
if error_compile:
raise Exception(f"Java code compilation error: {error_compile.decode('utf-8')}")
class_name = code.split("class")[1].split("{")[0].strip()
command_execute = f'java -classpath {compiled_file_path} {class_name}'
process_execute = subprocess.Popen(command_execute, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
output, error_execute = process_execute.communicate()
if error_execute:
raise Exception(f"An error occurred during Java code execution: {error_execute.decode('utf-8')}")
return output.decode('utf-8')
@app.route('/login')
def login():
return render_template('login.html')
@app.route('/admin')
def admin():
return render_template('admin.html')
@socketio.on('connect')
def on_connect():
# Add the current user's ID to the active_users set
active_users.add(request.sid)
emit('update_users', list(active_users), broadcast=True)
@socketio.on('disconnect')
def on_disconnect():
# Remove the current user's ID from the active_users set
active_users.discard(request.sid)
emit('update_users', list(active_users), broadcast=True)
@app.route('/signup')
def signup():
return render_template('signup.html')
@app.route('/restricted')
def restricted():
return render_template('restricted.html')
@app.errorhandler(404)
def page_not_found(error):
return render_template('404.html'), 404
# Add more routes and view functions for other HTML pages as needed
if __name__ == '__main__':
app.run(debug=True)