-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathv2
executable file
·56 lines (49 loc) · 1.51 KB
/
v2
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
#!./venv/bin/python3
import requests
import json
import argparse
import asyncio
import websockets
parser = argparse.ArgumentParser()
parser.add_argument("test_file")
parser.add_argument("--version", default="*")
parser.add_argument("--runtime")
parser.add_argument("--filename")
parser.add_argument("--mode", default="stateless")
parser.add_argument("--api", default="local")
args = parser.parse_args()
file_name = args.test_file
source_code_file = open(file_name)
language = file_name.split(".")[1]
source_code = source_code_file.read()
source_code_file.close()
base_url = "127.0.0.1:2000/api/v2"
ssl = ""
if args.api == "public":
base_url = "emkc.org/api/v2/piston"
ssl = "s"
request = {
"language": language,
"version": args.version,
"runtime": args.runtime,
"files": [{"name": args.filename, "content": source_code}],
}
async def ws_run():
async with websockets.connect(f"ws://{base_url}/connect") as websocket:
await websocket.send(json.dumps(request))
while True:
try:
res = await websocket.recv()
print(res)
except websockets.exceptions.ConnectionClosed as e:
print(e)
break
if args.mode == "stateless":
response = requests.post(f"http{ssl}://{base_url}/execute", json=request)
if (response.status_code >= 400):
print(response.status_code)
else:
print(json.dumps(response.json(), indent=4).replace("\\n", "\n"))
else:
request["type"] = "init"
asyncio.run(ws_run())