forked from QingdaoU/JudgeServer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.py
More file actions
145 lines (116 loc) · 4.52 KB
/
Copy pathclient.py
File metadata and controls
145 lines (116 loc) · 4.52 KB
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
import hashlib
import json
import requests
from languages import c_lang_config, cpp_lang_config, java_lang_config, c_lang_spj_config, \
c_lang_spj_compile, py2_lang_config, py3_lang_config
class JudgeServerClientError(Exception):
pass
class JudgeServerClient(object):
def __init__(self, token, server_base_url):
self.token = hashlib.sha256(token.encode("utf-8")).hexdigest()
self.server_base_url = server_base_url.rstrip("/")
def _request(self, url, data=None):
kwargs = {"headers": {"X-Judge-Server-Token": self.token,
"Content-Type": "application/json"}}
if data:
kwargs["data"] = json.dumps(data)
try:
return requests.post(url, **kwargs).json()
except Exception as e:
raise JudgeServerClientError(str(e))
def ping(self):
return self._request(self.server_base_url + "/ping")
def judge(self, src, language_config, max_cpu_time, max_memory, test_case_id, spj_version=None, spj_config=None,
spj_compile_config=None, spj_src=None, output=False):
data = {"language_config": language_config,
"src": src,
"max_cpu_time": max_cpu_time,
"max_memory": max_memory,
"test_case_id": test_case_id,
"spj_version": spj_version,
"spj_config": spj_config,
"spj_compile_config": spj_compile_config,
"spj_src": spj_src,
"output": output}
return self._request(self.server_base_url + "/judge", data=data)
def compile_spj(self, src, spj_version, spj_compile_config):
data = {"src": src, "spj_version": spj_version,
"spj_compile_config": spj_compile_config}
return self._request(self.server_base_url + "/compile_spj", data=data)
if __name__ == "__main__":
token = "YOUR_TOKEN_HERE"
c_src = r"""
#include <stdio.h>
int main(){
int a, b;
scanf("%d%d", &a, &b);
printf("%d\n", a+b);
return 0;
}
"""
c_spj_src = r"""
#include <stdio.h>
int main(){
return 1;
}
"""
cpp_src = r"""
#include <iostream>
using namespace std;
int main()
{
int a,b;
cin >> a >> b;
cout << a+b << endl;
return 0;
}
"""
java_src = r"""
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner in=new Scanner(System.in);
int a=in.nextInt();
int b=in.nextInt();
System.out.println(a + b);
}
}
"""
py2_src = """s = raw_input()
s1 = s.split(" ")
print int(s1[0]) + int(s1[1])"""
py3_src = """s = input()
s1 = s.split(" ")
print(int(s1[0]) + int(s1[1]))"""
client = JudgeServerClient(token=token, server_base_url="http://127.0.0.1:12358")
print("ping")
print(client.ping(), "\n\n")
print("compile_spj")
print(client.compile_spj(src=c_spj_src, spj_version="2", spj_compile_config=c_lang_spj_compile
), "\n\n")
print("c_judge")
print(client.judge(src=c_src, language_config=c_lang_config,
max_cpu_time=1000, max_memory=1024 * 1024 * 128,
test_case_id="normal", output=True), "\n\n")
print("cpp_judge")
print(client.judge(src=cpp_src, language_config=cpp_lang_config,
max_cpu_time=1000, max_memory=1024 * 1024 * 128,
test_case_id="normal"), "\n\n")
print("java_judge")
print(client.judge(src=java_src, language_config=java_lang_config,
max_cpu_time=1000, max_memory=256 * 1024 * 1024,
test_case_id="normal"), "\n\n")
print("c_spj_judge")
print(client.judge(src=c_src, language_config=c_lang_config,
max_cpu_time=1000, max_memory=1024 * 1024 * 128,
test_case_id="spj",
spj_version="3", spj_config=c_lang_spj_config,
spj_compile_config=c_lang_spj_compile, spj_src=c_spj_src), "\n\n")
print("py2_judge")
print(client.judge(src=py2_src, language_config=py2_lang_config,
max_cpu_time=1000, max_memory=128 * 1024 * 1024,
test_case_id="normal"), "\n\n")
print("py3_judge")
print(client.judge(src=py3_src, language_config=py3_lang_config,
max_cpu_time=1000, max_memory=128 * 1024 * 1024,
test_case_id="normal"), "\n\n")