mirror of
https://github.com/QingdaoU/JudgeServer.git
synced 2024-12-28 21:31:43 +00:00
使用http + json代替原有的rpc xml
This commit is contained in:
parent
0787243c55
commit
260b11b06d
@ -1,5 +1,5 @@
|
||||
FROM judger
|
||||
RUN apt-get update && apt-get install -y cmake vim
|
||||
RUN cd /tmp && rm -rf pyfadeaway && git clone https://github.com/nikoloss/pyfadeaway.git && cd pyfadeaway && python setup.py install
|
||||
RUN cd /tmp && rm -rf Judger && git clone https://github.com/QingdaoU/Judger.git && cd Judger && git checkout newnew && mkdir build && cd build && cmake .. && make && make install && cd ../bindings/Python && python setup.py install
|
||||
RUN pip install psutil
|
||||
RUN mkdir /var/wp
|
||||
RUN pip install psutil gunicorn web.py
|
||||
|
64
client.py
64
client.py
@ -1,33 +1,11 @@
|
||||
# coding=utf-8
|
||||
import hashlib
|
||||
import httplib
|
||||
import json
|
||||
import time
|
||||
import xmlrpclib
|
||||
|
||||
from utils import make_signature, check_signature
|
||||
|
||||
|
||||
class TimeoutHTTPConnection(httplib.HTTPConnection):
|
||||
def __init__(self, host, timeout=10):
|
||||
httplib.HTTPConnection.__init__(self, host, timeout=timeout)
|
||||
|
||||
|
||||
class TimeoutTransport(xmlrpclib.Transport):
|
||||
def __init__(self, timeout=10, *args, **kwargs):
|
||||
xmlrpclib.Transport.__init__(self, *args, **kwargs)
|
||||
self.timeout = timeout
|
||||
|
||||
def make_connection(self, host):
|
||||
conn = TimeoutHTTPConnection(host, self.timeout)
|
||||
return conn
|
||||
|
||||
|
||||
class TimeoutServerProxy(xmlrpclib.ServerProxy):
|
||||
def __init__(self, uri, timeout=10, *args, **kwargs):
|
||||
kwargs['transport'] = TimeoutTransport(timeout=timeout, use_datetime=kwargs.get('use_datetime', 0))
|
||||
xmlrpclib.ServerProxy.__init__(self, uri, *args, **kwargs)
|
||||
import requests
|
||||
|
||||
from utils import make_signature
|
||||
|
||||
c_lang_config = {
|
||||
"name": "c",
|
||||
@ -69,34 +47,28 @@ java_lang_config = {
|
||||
}
|
||||
|
||||
submission_id = str(int(time.time()))
|
||||
spj_config = c_lang_config["spj_compile"]
|
||||
|
||||
s = TimeoutServerProxy("http://192.168.99.100:8080", timeout=30, allow_none=True)
|
||||
|
||||
c_config = c_lang_config
|
||||
c_config["spj_compile"]["version"] = "1025"
|
||||
c_config["spj_compile"]["src"] = "#include<stdio.h>\nint main(){//哈哈哈哈\nreturn 0;}"
|
||||
|
||||
c_config["spj_compile"]["version"] = "1027"
|
||||
c_config["spj_compile"]["src"] = "#include<stdio.h>\nint main(){//哈哈哈哈\nwhile(1);return 0;}"
|
||||
|
||||
token = hashlib.sha256("token").hexdigest()
|
||||
|
||||
|
||||
def pong():
|
||||
data, signature, timestamp = s.pong()
|
||||
# check_signature(token=token, data=data, signature=signature, timestamp=timestamp)
|
||||
print json.loads(data)
|
||||
|
||||
|
||||
def judge():
|
||||
data, signature, timestamp = s.judge(*make_signature(token=token,
|
||||
language_config=c_lang_config,
|
||||
submission_id=submission_id,
|
||||
src="#include<stdio.h>\nint main(){//哈哈哈哈\nreturn 0;}",
|
||||
max_cpu_time=1000, max_memory=1000, test_case_id="1"))
|
||||
|
||||
# check_signature(token=token, data=data, signature=signature, timestamp=timestamp)
|
||||
print json.loads(data)
|
||||
data = make_signature(token=token,
|
||||
language_config=c_lang_config,
|
||||
submission_id=submission_id,
|
||||
src="#include<stdio.h>\nint main(){//哈哈哈哈\nwhile(1);return 0;}",
|
||||
max_cpu_time=1000, max_memory=1000 * 1024 * 1024,
|
||||
test_case_id="d28280c6f3c5ddeadfecc3956a52da3a")
|
||||
r = requests.post("http://192.168.99.100:8080/judge", data=json.dumps(data), headers={"content-type": "application/json"})
|
||||
print r.text
|
||||
|
||||
|
||||
pong()
|
||||
judge()
|
||||
def ping():
|
||||
r = requests.post("http://192.168.99.100:8080/ping", data=json.dumps({}), headers={"content-type": "application/json"})
|
||||
print r.text
|
||||
|
||||
ping()
|
||||
judge()
|
||||
|
@ -13,4 +13,4 @@ JUDGER_RUN_LOG_PATH = os.path.join(JUDGER_WORKSPACE_BASE, "judger.log")
|
||||
LOW_PRIVILEDGE_UID = pwd.getpwnam("nobody").pw_uid
|
||||
LOW_PRIVILEDGE_GID = grp.getgrnam("nogroup").gr_gid
|
||||
|
||||
TEST_CASE_DIR = "/var/testcase"
|
||||
TEST_CASE_DIR = "/test_case"
|
||||
|
@ -62,6 +62,7 @@ class JudgeClient(object):
|
||||
seccomp_rule_so_path=self._seccomp_rule_path(self._run_config["seccomp_rule"]),
|
||||
uid=LOW_PRIVILEDGE_UID,
|
||||
gid=LOW_PRIVILEDGE_GID)
|
||||
run_result["test_case"] = test_case_file_id
|
||||
return run_result
|
||||
|
||||
def run(self):
|
||||
|
61
server.py
61
server.py
@ -1,14 +1,13 @@
|
||||
# coding=utf-8
|
||||
from __future__ import unicode_literals
|
||||
|
||||
import SocketServer
|
||||
import hashlib
|
||||
import json
|
||||
import os
|
||||
import socket
|
||||
import shutil
|
||||
from SimpleXMLRPCServer import SimpleXMLRPCServer, SimpleXMLRPCRequestHandler
|
||||
|
||||
import web
|
||||
import _judger
|
||||
import psutil
|
||||
|
||||
@ -29,8 +28,7 @@ class InitSubmissionEnv(object):
|
||||
return self.path
|
||||
|
||||
def __exit__(self, exc_type, exc_val, exc_tb):
|
||||
pass
|
||||
# shutil.rmtree(self.path, ignore_errors=True)
|
||||
shutil.rmtree(self.path, ignore_errors=True)
|
||||
|
||||
|
||||
class JudgeServer(object):
|
||||
@ -54,16 +52,14 @@ class JudgeServer(object):
|
||||
|
||||
def judge(self, data, signature, timestamp):
|
||||
# check_signature(token=self._token, data=data, signature=signature, timestamp=timestamp)
|
||||
ret = {"code": None, "data": None}
|
||||
ret = {"err": None, "data": None}
|
||||
try:
|
||||
ret["data"] = self._judge(**json.loads(data))
|
||||
except (CompileError, SPJCompileError, SignatureVerificationFailed) as e:
|
||||
ret["code"] = e.__class__.__name__
|
||||
ret["err"] = e.__class__.__name__
|
||||
ret["data"] = e.message
|
||||
except Exception as e:
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
ret["code"] = "ServerError"
|
||||
ret["err"] = "ServerError"
|
||||
ret["data"] = ": ".join([e.__class__.__name__, e.message])
|
||||
return make_signature(token=self._token, **ret)
|
||||
|
||||
@ -89,21 +85,20 @@ class JudgeServer(object):
|
||||
max_cpu_time=max_cpu_time,
|
||||
max_memory=max_memory,
|
||||
test_case_id=test_case_id)
|
||||
return judge_client.run()
|
||||
run_result = judge_client.run()
|
||||
|
||||
if spj_compile_config:
|
||||
spj_compile_config["src_name"].format(spj_version=spj_compile_config["version"])
|
||||
spj_compile_config["exe_name"].format(spj_version=spj_compile_config["version"])
|
||||
spj_compile_config["src_name"] = spj_compile_config["src_name"].format(spj_version=spj_compile_config["version"])
|
||||
spj_compile_config["exe_name"] = spj_compile_config["exe_name"].format(spj_version=spj_compile_config["version"])
|
||||
|
||||
spj_src_path = os.path.join(TEST_CASE_DIR, test_case_id, spj_compile_config["src_name"])
|
||||
spj_exe_path = os.path.join(TEST_CASE_DIR, test_case_id, spj_compile_config["exe_name"])
|
||||
|
||||
# if spj source code not found, then write it into file
|
||||
if not os.path.exists(spj_src_path):
|
||||
with open(spj_src_path, "w") as f:
|
||||
f.write(spj_compile_config["src"].encode("utf-8"))
|
||||
|
||||
spj_exe_path = os.path.join(TEST_CASE_DIR, test_case_id, spj_compile_config["exe_name"])
|
||||
|
||||
# if spj exe file not found, then compile it
|
||||
if not os.path.exists(spj_exe_path):
|
||||
try:
|
||||
@ -113,17 +108,35 @@ class JudgeServer(object):
|
||||
# turn common CompileError into SPJCompileError
|
||||
except CompileError as e:
|
||||
raise SPJCompileError(e.message)
|
||||
return exe_path
|
||||
return run_result
|
||||
|
||||
def POST(self):
|
||||
try:
|
||||
data = json.loads(web.data())
|
||||
print web.ctx["path"]
|
||||
if web.ctx["path"] == "/judge":
|
||||
return self.judge(**data)
|
||||
if web.ctx["path"] == "/ping":
|
||||
return self.pong()
|
||||
return {"err": "invalid-method", "data": None}
|
||||
except Exception as e:
|
||||
ret = dict()
|
||||
ret["err"] = "ServerError"
|
||||
ret["data"] = ": ".join([e.__class__.__name__, e.message])
|
||||
return ret
|
||||
|
||||
|
||||
class RPCServer(SocketServer.ThreadingMixIn, SimpleXMLRPCServer):
|
||||
pass
|
||||
urls = (
|
||||
"/judge", "JudgeServer",
|
||||
"/ping", "JudgeServer"
|
||||
)
|
||||
|
||||
app = web.application(urls, globals())
|
||||
wsgiapp = app.wsgifunc()
|
||||
|
||||
if not os.environ.get("judger_token"):
|
||||
print "judger_token not set"
|
||||
exit(-1)
|
||||
|
||||
server = RPCServer(('0.0.0.0', 8080), SimpleXMLRPCRequestHandler, allow_none=True)
|
||||
server.register_instance(JudgeServer())
|
||||
server.serve_forever()
|
||||
# gunicorn -w 4 -b 0.0.0.0:8080 server:wsgiapp
|
||||
if __name__ == "__main__":
|
||||
if not os.environ.get("judger_token"):
|
||||
print "judger_token not set"
|
||||
exit(-1)
|
||||
app.run()
|
2
utils.py
2
utils.py
@ -12,7 +12,7 @@ def make_signature(**kwargs):
|
||||
token = kwargs.pop("token")
|
||||
data = json.dumps(kwargs)
|
||||
timestamp = int(time.time())
|
||||
return data, hashlib.sha256(data + str(timestamp) + token).hexdigest(), timestamp
|
||||
return {"data": data, "signature": hashlib.sha256(data + str(timestamp) + token).hexdigest(), "timestamp": timestamp}
|
||||
|
||||
|
||||
def check_signature(token, data, signature, timestamp):
|
||||
|
Loading…
Reference in New Issue
Block a user