Slide 89
Slide 89 text
サーバーソケットインターフェース
input_data = json.load(sys.stdin)
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
finish = False
s.bind((BIND_IP, BIND_PORT))
s.listen(1)
while not finish:
conn, addr = s.accept()
with conn:
while True:
data = conn.recv(MESSAGE_SIZE)
if data.decode('utf-8') == 'state':
conn.send(json.dumps(input_data).encode('utf-8'))
elif 'action' in json.loads(data.decode('utf-8')):
action = json.loads(data.decode('utf-8'))['action']
finish = True
...
json.dump(output_data, sys.stdout, indent = 4, ensure_ascii = False)