192 lines
6.2 KiB
Python
Raw Normal View History

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, go_lang_config, php_lang_config, js_lang_config
class JudgeServerClientError(Exception):
pass
class JudgeServerClient(object):
def __init__(self, token, server_base_url):
2017-04-27 14:33:38 +08:00
self.token = hashlib.sha256(token.encode("utf-8")).hexdigest()
self.server_base_url = server_base_url.rstrip("/")
def _request(self, url, data=None):
2016-10-25 23:08:17 +08:00
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:
2018-03-18 08:19:44 +08:00
raise JudgeServerClientError(str(e))
def ping(self):
return self._request(self.server_base_url + "/ping")
2018-12-12 14:27:40 +08:00
def judge(self, src, language_config, max_cpu_time, max_memory, test_case_id=None, test_case=None, spj_version=None, spj_config=None,
spj_compile_config=None, spj_src=None, output=False):
2018-12-12 14:27:40 +08:00
if not (test_case or test_case_id) or (test_case and test_case_id):
raise ValueError("invalid parameter")
data = {"language_config": language_config,
"src": src,
"max_cpu_time": max_cpu_time,
"max_memory": max_memory,
2016-10-02 02:05:16 +08:00
"test_case_id": test_case_id,
2018-12-12 14:27:40 +08:00
"test_case": test_case,
2016-10-02 02:05:16 +08:00
"spj_version": spj_version,
2016-10-10 00:00:54 +08:00
"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)
2017-11-25 19:27:08 +08:00
def compile_spj(self, src, spj_version, spj_compile_config):
data = {"src": src, "spj_version": spj_version,
2017-11-25 19:27:08 +08:00
"spj_compile_config": spj_compile_config}
return self._request(self.server_base_url + "/compile_spj", data=data)
if __name__ == "__main__":
2017-12-22 14:02:37 +08:00
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;
}
"""
2016-10-02 02:05:16 +08:00
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);
}
}
"""
2016-10-09 20:05:04 +08:00
py2_src = """s = raw_input()
s1 = s.split(" ")
print int(s1[0]) + int(s1[1])"""
2017-04-29 18:30:16 +08:00
py3_src = """s = input()
s1 = s.split(" ")
print(int(s1[0]) + int(s1[1]))"""
2020-07-08 01:30:49 +00:00
go_src = """package main
import "fmt"
func main() {
2020-07-09 08:21:57 +08:00
a := 0
b := 0
fmt.Scanf("%d %d", &a, &b)
fmt.Printf("%d", a + b)
2020-07-08 01:30:49 +00:00
}"""
php_src = """<?php
fscanf(STDIN, "%d %d", $a, $b);
print($a + $b);"""
js_src = """const readline = require('readline');
const rl = readline.createInterface({ input: process.stdin });
rl.on('line', (input) => {
if (input === '') {
return rl.close();
}
const [a, b] = input.split(' ').map(Number)
console.log(a + b);
});"""
2017-11-25 19:27:08 +08:00
client = JudgeServerClient(token=token, server_base_url="http://127.0.0.1:12358")
print("ping")
2017-04-27 14:33:38 +08:00
print(client.ping(), "\n\n")
2016-10-09 20:05:04 +08:00
2017-11-25 19:27:08 +08:00
print("compile_spj")
print(client.compile_spj(src=c_spj_src, spj_version="2", spj_compile_config=c_lang_spj_compile
), "\n\n")
2017-11-25 19:27:08 +08:00
print("c_judge")
2017-04-27 14:33:38 +08:00
print(client.judge(src=c_src, language_config=c_lang_config,
max_cpu_time=1000, max_memory=1024 * 1024 * 128,
2017-04-27 14:33:38 +08:00
test_case_id="normal", output=True), "\n\n")
2016-10-02 02:05:16 +08:00
2017-11-25 19:27:08 +08:00
print("cpp_judge")
2017-04-27 14:33:38 +08:00
print(client.judge(src=cpp_src, language_config=cpp_lang_config,
2016-10-02 02:05:16 +08:00
max_cpu_time=1000, max_memory=1024 * 1024 * 128,
2017-04-27 14:33:38 +08:00
test_case_id="normal"), "\n\n")
2016-10-02 02:05:16 +08:00
2017-11-25 19:27:08 +08:00
print("java_judge")
2017-04-27 14:33:38 +08:00
print(client.judge(src=java_src, language_config=java_lang_config,
2017-11-25 19:27:08 +08:00
max_cpu_time=1000, max_memory=256 * 1024 * 1024,
2017-04-27 14:33:38 +08:00
test_case_id="normal"), "\n\n")
2016-10-02 02:05:16 +08:00
2017-11-25 19:27:08 +08:00
print("c_spj_judge")
2017-04-27 14:33:38 +08:00
print(client.judge(src=c_src, language_config=c_lang_config,
max_cpu_time=1000, max_memory=1024 * 1024 * 128,
2016-10-02 02:05:16 +08:00
test_case_id="spj",
2016-10-10 00:00:54 +08:00
spj_version="3", spj_config=c_lang_spj_config,
2017-04-27 14:33:38 +08:00
spj_compile_config=c_lang_spj_compile, spj_src=c_spj_src), "\n\n")
2016-10-10 00:00:54 +08:00
2017-11-25 19:27:08 +08:00
print("py2_judge")
2017-04-27 14:33:38 +08:00
print(client.judge(src=py2_src, language_config=py2_lang_config,
2016-10-10 00:00:54 +08:00
max_cpu_time=1000, max_memory=128 * 1024 * 1024,
2020-07-08 01:30:49 +00:00
test_case_id="normal", output=True), "\n\n")
2017-04-29 18:30:16 +08:00
2017-11-25 19:27:08 +08:00
print("py3_judge")
2017-04-29 18:30:16 +08:00
print(client.judge(src=py3_src, language_config=py3_lang_config,
max_cpu_time=1000, max_memory=128 * 1024 * 1024,
2020-07-08 01:30:49 +00:00
test_case_id="normal", output=True), "\n\n")
print("go_judge")
print(client.judge(src=go_src, language_config=go_lang_config,
max_cpu_time=1000, max_memory=128 * 1024 * 1024,
test_case_id="normal", output=True), "\n\n")
2018-12-12 14:27:40 +08:00
print("php_judge")
print(client.judge(src=php_src, language_config=php_lang_config,
max_cpu_time=1000, max_memory=128 * 1024 * 1024,
test_case_id="normal", output=True), "\n\n")
print("js_judge")
print(client.judge(src=js_src, language_config=js_lang_config,
max_cpu_time=1000, max_memory=128 * 1024 * 1024,
test_case_id="normal", output=True), "\n\n")
2018-12-12 14:27:40 +08:00
print("c_dynamic_input_judge")
print(client.judge(src=c_src, language_config=c_lang_config,
max_cpu_time=1000, max_memory=1024 * 1024 * 128,
test_case=[{"input": "1 2\n", "output": "3"}, {"input": "1 4\n", "output": "3"}], output=True), "\n\n")