mirror of
https://github.com/QingdaoU/JudgeServer.git
synced 2024-12-28 21:31:43 +00:00
使用面向对象的风格改写client
This commit is contained in:
parent
0dab860ea4
commit
3e89ac6056
82
client.py
82
client.py
@ -1,82 +0,0 @@
|
||||
# coding=utf-8
|
||||
from __future__ import unicode_literals
|
||||
|
||||
import hashlib
|
||||
import json
|
||||
import time
|
||||
|
||||
import requests
|
||||
|
||||
from languages import c_lang_config, cpp_lang_config, java_lang_config
|
||||
|
||||
token = hashlib.sha256("token").hexdigest()
|
||||
|
||||
c_src = r"""
|
||||
#include <stdio.h>
|
||||
int main(){
|
||||
int a, b;
|
||||
scanf("%d%d", &a, &b);
|
||||
printf("%d\n", a+b);
|
||||
return 0;
|
||||
}
|
||||
"""
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
"""
|
||||
|
||||
|
||||
def compile_spj(src, spj_version, spj_config, test_case_id):
|
||||
r = requests.post("http://123.57.151.42:11235/compile_spj",
|
||||
data=json.dumps({"src": src, "spj_version": spj_version,
|
||||
"spj_compile_config": spj_config, "test_case_id": test_case_id}),
|
||||
headers={"X-Judge-Server-Token": token})
|
||||
return r.json()
|
||||
|
||||
|
||||
def judge(src, language_config, submission_id):
|
||||
data = {"language_config": language_config,
|
||||
"submission_id": submission_id,
|
||||
"src": src,
|
||||
"max_cpu_time": 1000,
|
||||
"max_memory": 1024 * 1024 * 1024,
|
||||
"test_case_id": "d8c460de943189a83bad166ec96d975d"}
|
||||
r = requests.post("http://123.57.151.42:11235/judge", data=json.dumps(data), headers={"X-Judge-Server-Token": token})
|
||||
return r.json()
|
||||
|
||||
|
||||
def ping():
|
||||
r = requests.post("http://123.57.151.42:11235/ping", headers={"X-Judge-Server-Token": token})
|
||||
return r.json()
|
||||
|
||||
|
||||
print ping()
|
||||
print compile_spj(src=c_src, spj_version="2", spj_config=c_lang_config["spj"], test_case_id="d8c460de943189a83bad166ec96d975d")
|
||||
|
||||
print judge(src=c_src, language_config=c_lang_config, submission_id=str(int(time.time())))
|
||||
time.sleep(2)
|
||||
print judge(src=cpp_src, language_config=cpp_lang_config, submission_id=str(int(time.time())))
|
||||
time.sleep(2)
|
||||
print judge(src=java_src, language_config=java_lang_config, submission_id=str(int(time.time())))
|
0
client/Python/__init__.py
Normal file
0
client/Python/__init__.py
Normal file
100
client/Python/client.py
Normal file
100
client/Python/client.py
Normal file
@ -0,0 +1,100 @@
|
||||
# coding=utf-8
|
||||
from __future__ import unicode_literals
|
||||
|
||||
import hashlib
|
||||
import json
|
||||
import time
|
||||
|
||||
import requests
|
||||
|
||||
from languages import c_lang_config, cpp_lang_config, java_lang_config
|
||||
|
||||
|
||||
class JudgeServerClientError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class JudgeServerClient(object):
|
||||
def __init__(self, token, server_base_url):
|
||||
self.token = hashlib.sha256(token).hexdigest()
|
||||
self.server_base_url = server_base_url.rstrip("/")
|
||||
|
||||
def _request(self, url, data=None):
|
||||
kwargs = {"headers": {"X-Judge-Server-Token": self.token}}
|
||||
if data:
|
||||
kwargs["data"] = json.dumps(data)
|
||||
try:
|
||||
return requests.post(url, **kwargs).json()
|
||||
except Exception as e:
|
||||
raise JudgeServerClientError(e.message)
|
||||
|
||||
def ping(self):
|
||||
return self._request(self.server_base_url + "/ping")
|
||||
|
||||
def judge(self, src, language_config, submission_id, max_cpu_time, max_memory, test_case_id):
|
||||
data = {"language_config": language_config,
|
||||
"submission_id": submission_id,
|
||||
"src": src,
|
||||
"max_cpu_time": max_cpu_time,
|
||||
"max_memory": max_memory,
|
||||
"test_case_id": test_case_id}
|
||||
return self._request(self.server_base_url + "/judge", data=data)
|
||||
|
||||
def compile_spj(self, src, spj_version, spj_config, test_case_id):
|
||||
data = {"src": src, "spj_version": spj_version,
|
||||
"spj_compile_config": spj_config, "test_case_id": test_case_id}
|
||||
return self._request(self.server_base_url + "/compile_spj", data=data)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
token = hashlib.sha256("token").hexdigest()
|
||||
|
||||
c_src = r"""
|
||||
#include <stdio.h>
|
||||
int main(){
|
||||
int a, b;
|
||||
scanf("%d%d", &a, &b);
|
||||
printf("%d\n", a+b);
|
||||
return 0;
|
||||
}
|
||||
"""
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
"""
|
||||
|
||||
client = JudgeServerClient(token="token", server_base_url="http://123.57.151.42:11235")
|
||||
print client.ping(), "\n\n"
|
||||
print client.compile_spj(src=c_src, spj_version="4", spj_config=c_lang_config["spj"],
|
||||
test_case_id="d8c460de943189a83bad166ec96d975d"), "\n\n"
|
||||
|
||||
print client.judge(src=cpp_src, language_config=cpp_lang_config, submission_id=str(int(time.time())),
|
||||
max_cpu_time=1000, max_memory=1024 * 1024 * 128,
|
||||
test_case_id="d8c460de943189a83bad166ec96d975d"), "\n\n"
|
||||
time.sleep(2)
|
||||
print client.judge(src=java_src, language_config=java_lang_config, submission_id=str(int(time.time())),
|
||||
max_cpu_time=1000, max_memory=1024 * 1024 * 128,
|
||||
test_case_id="d8c460de943189a83bad166ec96d975d"), "\n\n"
|
||||
time.sleep(2)
|
Loading…
Reference in New Issue
Block a user