-
Notifications
You must be signed in to change notification settings - Fork 1
/
oj.py
105 lines (92 loc) · 3.23 KB
/
oj.py
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
import http.client
import json
class OJClient:
def __init__(self, config):
self.config = config
def getHost(self):
return self.config["host"];
def getAPI(self, apiName):
return "%s/%s" % (self.config["requestpath"], apiName)
def getPostAPI(self, apiName):
return "%s/%s?apikey=%s" % (self.config["requestpath"], apiName, self.config["apikey"])
def getHTTPConnection(self):
return http.client.HTTPConnection(self.config["host"])
def getHTTPRequest(self, conn, path, body):
conn.request("POST", path, body)
return conn.getresponse()
def checkHTTPResponse(self, response):
if(response.status != 200):
print(response.status)
print(response.reason)
exit()
def closeHTTPConnection(self, conn):
conn.close()
def getJSONRequest(self, path, postdata):
conn = self.getHTTPConnection()
resp = self.getHTTPRequest(conn, path, postdata)
self.checkHTTPResponse(resp)
__data = resp.read()
self.closeHTTPConnection(conn)
return json.loads(bytes.decode(__data))
def getTextRequest(self, path, postdata):
conn = self.getHTTPConnection()
resp = self.getHTTPRequest(conn, path, postdata)
self.checkHTTPResponse(resp)
__data = resp.read()
self.closeHTTPConnection(conn)
return __data
def Verify(self):
APIKey = self.config["apikey"]
path = self.getAPI("verify")
__data = self.getJSONRequest(path, str.encode(json.dumps({
"apikey" : APIKey
})));
return __data["status"] == "success"
def GetTask(self):
APIKey = self.config["apikey"]
path = self.getPostAPI("task")
__data = self.getJSONRequest(path, str.encode(json.dumps({
"type" : "get"
})));
return __data
def GetCode(self, sid):
APIKey = self.config["apikey"]
path = self.getPostAPI("code")
__data = self.getTextRequest(path, str.encode(json.dumps({
"type" : "get",
"sid" : sid
})));
return __data
def GetData(self, pdid, ext):
APIKey = self.config["apikey"]
path = self.getPostAPI("data")
__data = self.getTextRequest(path, str.encode(json.dumps({
"type" : "get",
"pdid" : pdid,
"ext" : ext
})));
return __data
def GetDataList(self, pid):
APIKey = self.config["apikey"]
path = self.getPostAPI("data")
__data = self.getJSONRequest(path, str.encode(json.dumps({
"type" : "list",
"pid" : pid
})));
return __data
def PutRet(self, sid, data):
APIKey = self.config["apikey"]
path = self.getPostAPI("post/%d" % (sid))
__data = self.getJSONRequest(path, data);
return __data
def PutStatus(self, sid, status, mem, time):
APIKey = self.config["apikey"]
path = self.getPostAPI("status")
__data = self.getJSONRequest(path, str.encode(json.dumps({
"type" : "set",
"sid" : sid,
"status" : status,
"memlimit" : mem,
"timelimit" : time
})));
return __data