-
Notifications
You must be signed in to change notification settings - Fork 0
/
ffctrl.py
30 lines (27 loc) · 931 Bytes
/
ffctrl.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
import json
import socket
# Author: chrn (original by nneonneo)
# Date: 11.11.2016
# Copyright: https://github.com/nneonneo/2048-ai
# Description: Handles the communication with the firefox browser.
class FirefoxRemoteControl(object):
#Interact with a web browser running the Remote Control extension.
def __init__(self, port):
self.sock = socket.socket()
self.sock.connect(('localhost', port))
def execute(self, cmd):
msg = cmd.replace('\n', ' ') + '\r\n'
self.sock.send(msg.encode('utf8'))
ret = []
while True:
chunk = self.sock.recv(4096)
ret.append(chunk)
if b'\n' in chunk:
break
res = json.loads(b''.join(ret).decode('utf8'))
if 'error' in res:
raise Exception(res['error'])
elif not res:
return None
else:
return res['result']