forked from PyJaipur/PyJudge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
56 lines (44 loc) · 1.76 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
from bottle import Bottle, run, template, static_file, request, route
import os
import sys
import datetime
from collections import defaultdict, namedtuple
path = os.path.abspath(__file__)
dir_path = os.path.dirname(path)
app = Bottle()
questions = {}
usernames = defaultdict(list) # dictionary for storing the usernames
question_dir = 'files/questions'
Question = namedtuple('Question', 'output statement')
Submission = namedtuple('Submission', 'question time result output')
for i in os.listdir(question_dir):
# read the correct output as bytes object
with open(os.path.join(question_dir, i, 'output.txt'), 'rb') as fl:
output = fl.read()
with open(os.path.join(question_dir, i, 'statement.txt'), 'r') as fl:
statement = fl.read()
questions[i] = Question(output=output, statement=statement)
@app.get('/question/<number>')
def question(number):
statement = questions[number].statement
return template('index.html', question_number=number, question=statement)
@app.get('/question/<path:path>')
def download(path):
return static_file(path, root=question_dir)
@app.post('/check/<number>')
def file_upload(number):
u_name = request.forms.get('username') # accepting username
time = datetime.datetime.now()
# type(uploaded) == <class 'bytes'>
uploaded = request.files.get('upload').file.read() # uploaded outputs by user
expected = questions[number].output
expected = expected.strip()
uploaded = uploaded.strip()
ans = (uploaded == expected)
usernames[u_name].append(Submission(question=number, time=time,
output=uploaded, result=ans))
if not ans:
return "Wrong Answer!!"
else:
return "Solved! Great Job! "
run(app, host='localhost', port=8080)