我想使用来自 Python的 Chrome 开发工具(链接)的远程套接字调试东西。我正在使用从这里改编的代码。
我设法得到ping
并list_tabs
工作。但我不知道该怎么做evaluate_javascript
。谁能告诉我我做错了什么?
import subprocess
import time, json, socket
from jca.files import my_paths
def request(tool, destination=None, **kw):
# Send a command via socket to 'DevToolsService' or 'V8Debugger'
j = json.dumps(kw)
request = 'Content-Length:%d\r\nTool:%s\r\n' % (len(j), tool)
if destination:
request += 'Destination:%s\r\n' % (destination,)
request += '\r\n%s\r\n' % (j,)
sock.send(request)
if kw.get('command', '') not in RESPONSELESS_COMMANDS:
time.sleep(.1)
response = sock.recv(30000)
if response.strip():
j = response.split('\r\n\r\n', 1)[1]
return json.loads(j)
if __name__ == '__main__':
proc = subprocess.Popen('"%s" --remote-shell-port=9222' % my_paths.chrome_exe)
RESPONSELESS_COMMANDS = ['evaluate_javascript']
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(('localhost', 9222))
sock.send('ChromeDevToolsHandshake\r\n')
result = sock.recv(1024)
print 'ping: ', request('DevToolsService', command='ping')
time.sleep(4)
print 'list_tabs: ', request('DevToolsService', command='list_tabs')
request('V8Debugger', command='evaluate_javascript',
data='javascript:window.location.reload()')
sock.close()
print 'done'