-
Notifications
You must be signed in to change notification settings - Fork 3
/
chromeremote.py
349 lines (294 loc) · 11 KB
/
chromeremote.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import requests
import websocket
import threading
import time
from functools import partial
import json
import traceback
import warnings
class WSMethod(object):
def __init__(self, name, parent):
self.name = name
self.parent = parent
def __getattr__(self, attr):
func_name = self.name + "." + attr
return partial(self.parent.ws_send, func_name)
class TabTimeout(Exception):
pass
class Result(object):
def __init__(self, message_id, func_name, callback=None, on_error=None):
self.message_id = message_id
self.func_name = func_name
self.ready = False
self.result = None
self.callback = callback
self.on_error = on_error
def set_result(self, result):
self.result = result
self.ready = True
def __str__(self):
return ("<Result object <message id>: %s, "
"<method>: %s, "
"<ready>: %s, "
"<result>: %s> "
"<callback>: %s> "
"<on_error>: %s> ") % (self.message_id, self.func_name,
self.ready, self.result, self.callback,
self.on_error)
class ChromeDevToolsConnection(object):
def __init__(self, host, port):
self.host = host
self.port = port
def send_commad(self, method='', arg='', data=None):
if not method:
url = "http://{host}:{port}/json"
else:
url = "http://{host}:{port}/json/{method}/"
if arg:
url += "{arg}"
url = url.format(
host=self.host,
port=self.port,
method=method,
arg=arg,
)
if data is None:
resp = requests.get(url)
else:
resp = requests.post(url, data)
if resp.status_code >= 400:
raise requests.HTTPError("<code>: {}, <mesage>:{}.".format(
resp.status_code, resp.text))
return resp
def get_tabs(self):
resp = self.list()
return resp.json()
def new_tab(self, tab_info=None):
tab = ChromeTab(connection=self)
tab.open_tab(tab_info)
return tab
def __getattr__(self, func_name):
return partial(self.send_commad, func_name)
class ChromeTab(object):
WS_TIMEOUT = 0.2
TAB_TIMEOUT = 60 * 60 * 2 # Close this tab after 2 hours.
def __init__(self, host=None, port=None, connection=None, ws_timeout=0.2):
if isinstance(connection, ChromeDevToolsConnection):
# Using existing ChromeDevToolsConnection instance
self.conn = connection
else:
# Initialize a new ChromeDevToolsConnection instance
host = host or '127.0.0.1'
port = port or 9222
self.conn = ChromeDevToolsConnection(host, port)
self.message_id = 0
self.msg_lok = threading.Lock()
self.tab_id = None
self.ws = None
self.tab_info = None
self.ws_timeout = ws_timeout or self.WS_TIMEOUT
self.start_time = 0
self._running = True
self._message_callbacks = {}
self._events_callbacks = {}
def get_message_id(self):
self.msg_lok.acquire()
self.message_id += 1
msg_id = self.message_id
self.msg_lok.release()
return msg_id
def _add_message_callback(self,
message_id,
func_name,
callback,
on_error=None):
res = Result(message_id, func_name, callback, on_error=on_error)
self._message_callbacks[message_id] = res
return res
def open_tab(self, tab_info=None, connect_ws=True):
'''
Open a new tab if tab_info not provided.
"tab_info" if a dict get from http://{host}:{port}/json
'''
if tab_info:
if not (isinstance(tab_info, dict) and 'id' in tab_info):
raise ValueError(
"'tab_info' must be a dict and contains key 'id'.")
else:
if 'webSocketDebuggerUrl' not in tab_info:
raise ValueError(
"'tab_info' has no 'webSocketDebuggerUrl', maybe Its in use."
)
else:
resp = self.conn.new()
tab_info = resp.json()
self.tab_info = tab_info
self.tab_id = tab_info['id']
if connect_ws:
self.connect_ws(self.ws_url)
return tab_info
@property
def ws_url(self):
return self.tab_info and self.tab_info['webSocketDebuggerUrl']
def connect_ws(self, ws_url=None):
if self.ws:
self.close_ws()
ws_url = ws_url or self.ws_url
if not ws_url:
raise ValueError("'ws_url' must be given.")
self.ws = websocket.create_connection(ws_url)
self.ws.settimeout(self.ws_timeout)
def ws_send(self, func_name, **kwargs):
callback = kwargs.pop('callback', None)
on_error = kwargs.pop('on_error', None)
message_id = self.get_message_id()
call_obj = {"id": message_id, "method": func_name, "params": kwargs}
self.ws.send(json.dumps(call_obj))
result = self._add_message_callback(
message_id, func_name, callback, on_error=on_error)
return result
def register_event(self, event_name, function):
if not callable(function):
raise TypeError("function must be a callable.")
self._events_callbacks[event_name] = function
def unregister_event(self, event_name):
return self._events_callbacks.pop(event_name, None)
def get_status(self):
"Return this tab is busy not not."
raise NotImplementedError
def close_ws(self):
if not self.ws:
return
try:
self.ws.close()
except websocket.WebSocketConnectionClosedException:
pass
def close_tab(self):
closed = False
self._running = False
if self.ws:
self.close_ws()
try:
resp = self.conn.close(self.tab_id)
if "Target is closing" in resp.text:
closed = True
except:
closed = False
return closed
def kill(self):
self.close_tab()
def __getattr__(self, attr):
self.check_ws_ready()
genericelement = WSMethod(attr, self)
self.__setattr__(attr, genericelement)
return genericelement
def default_on_error(self, id, func_name, error):
print("Message id: {} received an error. "
"function name: {}, error message: {}. "
"Provide an 'on_error' function to handle this error yourself.".
format(id, func_name, error))
def handle_message_callback(self, message_id, result=None, error=None):
# print "length of message_callbacks", len(self._message_callbacks)
if message_id not in self._message_callbacks:
raise ValueError
callback_result = self._message_callbacks.pop(message_id)
try:
callback_result.set_result(result and result.copy())
if result is not None:
result['current_tab'] = self
if not callback_result.callback:
return
callback_result.callback(result)
if error is not None:
error['current_tab'] = self
if not callback_result.on_error:
self.default_on_error(message_id,
callback_result.func_name, error)
return
callback_result.on_error(error)
except Exception as e:
msg = ("Got an exception in message callback, "
"<message id>:{} ,"
"<request function>:{} ,"
"<callback function>:{} "
"<on_error function>:{} ")
print(msg.format(message_id, callback_result.func_name,
callback_result.callback,
callback_result.on_error))
traceback.print_exc()
def handle_event_callback(self, event_name, params):
if event_name not in self._events_callbacks:
return
func = self._events_callbacks[event_name]
try:
params['current_tab'] = self
func(params)
except Exception as e:
msg = ("Got an exception in event callback, "
"<event name>:{} ,"
"<callback function>:{} ")
print(msg.format(event_name, func))
traceback.print_exc()
def handle_messages(self, parsed_message):
if 'id' in parsed_message and ('result' in parsed_message
or 'error' in parsed_message):
self.handle_message_callback(
parsed_message['id'],
result=parsed_message.get('result', None),
error=parsed_message.get('error', None))
elif 'method' in parsed_message and 'params' in parsed_message:
self.handle_event_callback(parsed_message['method'],
parsed_message['params'])
def check_ws_ready(self):
if not self.ws:
raise ValueError("Tab is not initialized.")
def messages(self, auto_handle_message=True, timeout_return_none=False):
self.start_time = time.time()
self.check_ws_ready()
while self._running:
# check tab max timeout
now = time.time()
if now - self.start_time > self.TAB_TIMEOUT:
raise TabTimeout()
try:
message = self.ws.recv()
parsed_message = json.loads(message)
if auto_handle_message:
self.handle_messages(parsed_message)
yield parsed_message
except websocket.WebSocketTimeoutException:
if timeout_return_none:
yield None
continue
except websocket.WebSocketConnectionClosedException:
break
except KeyboardInterrupt:
break
def run(self):
if not self.tab_id and not self.ws:
self.open_tab()
for i in self.messages(auto_handle_message=False):
self.handle_messages(i)
self.close_tab()
class ChromeTabThread(ChromeTab, threading.Thread):
def __init__(self, host=None, port=None, connection=None, ws_timeout=0.2):
super(ChromeTabThread, self).__init__(host, port, connection,
ws_timeout)
super(ChromeTab, self).__init__()
def run(self):
if not self.tab_id and not self.ws:
self.open_tab()
for i in self.messages():
pass
self.close_tab()
def main():
w1 = ChromeTabThread('127.0.0.1', 9222)
w1.start()
time.sleep(2)
w1.kill()
time.sleep(1)
print(w1.isAlive())
if __name__ == '__main__':
main()