def server(path): if path in ("judge", "ping", "compile_spj"): logger.warning("app reached" + path) print("app reached," + path) _token = request.headers.get("X-Judge-Server-Token") try: if _token != token: raise TokenVerificationFailed("invalid token") try: data = request.json except Exception: data = {} ret = {"err": None, "data": getattr(JudgeServer, path)(**data)} except (CompileError, TokenVerificationFailed, SPJCompileError, JudgeClientError) as e: logger.exception(e) ret = {"err": e.__class__.__name__, "data": e.message} except Exception as e: logger.exception(e) ret = { "err": "JudgeClientError", "data": e.__class__.__name__ + " :" + str(e) } else: ret = {"err": "InvalidRequest", "data": "404"} return Response(json.dumps(ret), mimetype='application/json')
def server(path): #设置多个可能的路由请求 if path in ("judge", "ping", "compile_spj"): _token = request.headers.get("X-Judge-Server-Token") try: #如果令牌出错就抛出异常,否者执行try,获取数据 if _token != token: raise TokenVerificationFailed("invalid token") try: data = request.json except Exception: data = {} #**将data的json格式数据转换成a=b的形式 ret = {"err": None, "data": getattr(JudgeServer, path)(**data)} except (CompileError, TokenVerificationFailed, SPJCompileError, JudgeClientError) as e: logger.exception(e) ret = {"err": e.__class__.__name__, "data": e.message} except Exception as e: logger.exception(e) ret = { "err": "JudgeClientError", "data": e.__class__.__name__ + " :" + str(e) } else: ret = {"err": "InvalidRequest", "data": "404"} #通过json的dumps的模块可以把特定的对象序列化处理为字符串 return Response(json.dumps(ret), mimetype='application/json')
def POST(self): _token = web.ctx.env.get("HTTP_X_JUDGE_SERVER_TOKEN", None) try: if _token != token: raise TokenVerificationFailed("invalid token") if web.data(): try: data = json.loads(web.data()) except Exception as e: logger.info(web.data()) return {"ret": "ServerError", "data": "invalid json"} else: data = {} if web.ctx["path"] == "/judge": callback = self.judge elif web.ctx["path"] == "/ping": callback = self.pong elif web.ctx["path"] == "/compile_spj": callback = self.compile_spj else: return json.dumps({"err": "InvalidMethod", "data": None}) return json.dumps({"err": None, "data": callback(**data)}) except (CompileError, TokenVerificationFailed, SPJCompileError, JudgeClientError) as e: logger.exception(e) ret = dict() ret["err"] = e.__class__.__name__ ret["data"] = e.message return json.dumps(ret) except Exception as e: logger.exception(e) ret = dict() ret["err"] = "JudgeClientError" ret["data"] = e.__class__.__name__ + ":" + e.message return json.dumps(ret)
def server(path): if path in {"judge", "ping", "compile_spj"}: _token = request.headers.get("X-Judge-Server-Token") try: if _token != token: raise TokenVerificationFailed("invalid token") try: data = request.json except Exception: data = {} status = 200 ret = {"err": None, "data": getattr(JudgeServer, path)(**data)} except (CompileError, CompilerRuntimeError, TokenVerificationFailed, SPJCompileError, JudgeClientError) as e: status = e.status logger.exception(e) ret = {"err": e.__class__.__name__, "data": e.message} except Exception as e: status = 500 logger.exception(e) ret = {"err": "JudgeClientError", "data": e.__class__.__name__ + " :" + str(e)} else: status = 400 ret = {"err": "InvalidRequest", "data": "404"} return Response(json.dumps(ret), mimetype='application/json', status=status)
def get_token(): TOKEN = os.environ.get('TOKEN') if not TOKEN: raise TokenVerificationFailed("Error no TOKEN was found!") else: return TOKEN