-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttpapi.py
More file actions
72 lines (52 loc) · 1.84 KB
/
httpapi.py
File metadata and controls
72 lines (52 loc) · 1.84 KB
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
from subprocess import Popen, PIPE
from flask import Flask, jsonify, request, Response
KELVANDOR = './kelvandor'
MAX_ITERATIONS = 300000
MIN_ITERATIONS = 100
app = Flask(__name__)
@app.route('/<board>', methods=['OPTIONS'])
def options(board):
response = Response()
response.headers['Access-Control-Allow-Origin'] = '*'
response.headers['Access-Control-Allow-Headers'] = 'Iterations, Mode'
return response
@app.route('/<board>')
def think(board):
assert board.isalnum()
args = []
mode = 'normal'
if 'Iterations' in request.headers:
try:
args += ['-i', str(
max(min(int(request.headers['Iterations']),
MAX_ITERATIONS), MIN_ITERATIONS)
)]
except ValueError:
pass
if 'Mode' in request.headers:
if request.headers['Mode'] == 'move scan':
args += ['-m']
mode = 'move scan'
p = Popen([KELVANDOR] + args + [board], stdout=PIPE, stderr=PIPE)
if mode == 'move scan':
actions = []
for line in p.stdout.readlines():
action, value = line.strip().decode('utf-8').split('\t')
actions.append({'action': action, 'value': value})
response = jsonify(actions)
response.headers['Access-Control-Allow-Origin'] = '*'
response.headers['Access-Control-Allow-Headers'] = 'Iterations, Move'
return response
actions = []
for action in p.stdout.readlines():
actions.append(action.strip().decode('utf-8'))
log = p.stderr.read().decode('utf-8')
with open('httpapi.log', 'a') as f:
f.write(log)
response = jsonify({
'actions': actions,
'log': log,
})
response.headers['Access-Control-Allow-Origin'] = '*'
response.headers['Access-Control-Allow-Headers'] = 'Iterations, Move'
return response