Is it possible to connect to the remote protocol of the Mobile Safari debugger using python?

I have an HTML5-based application running on iOS and I want to connect to it using the webkit 1 remote debugger protocol, which is now supported on iOS 5 2 .

I am trying to track down a problem when my javascript application crashes the browser heavily (SEG_FAULT). I would like to get a trace of the application as it runs, so that I can see which lines or network operations might lead to this problem. My idea is to write a python application that will connect to a remote debugger and continue to execute code and collect information in a log file while I interact with the application.

I ran into an initial hurdle, although I cannot find any examples or documentation on how to connect to the debugger and communicate, or even if possible.

Does anyone know if this is possible, and if so, can you give me some documentation and / or sample code?


Based on the code from below, I also created a github project to test some ideas. You can find it here: abierbaum: / python_webkit-remote_debugger

+6
source share
1 answer

Yes, if you included the inspector in your UIWebView following the instructions, it can be connected to Python. I played with him and figured out how to send and receive commands using the web jack. Here's a script for Python 2.7 using websocket-client

import json import socket from websocket import WebSocket ws = WebSocket() # if ipv6 ws.io_sock = ws.sock = socket.socket(socket.AF_INET6, socket.SOCK_STREAM) ws.connect("ws://localhost:9999/devtools/page/1") counter = 0 def send(method, params): global counter counter += 1 # separators is important, you'll get "Message should be in JSON format." otherwise message = json.dumps({"id": counter, "method": method, "params": params}, separators=(',', ':')) print "> %s" % (message,) ws.send(message) def recv(): result = ws.recv() print "< %s" % (result,) send('Runtime.evaluate', {'expression': 'alert("hello from python")'}) recv() 

The Runtime.evaluate function is used to display a warning.

I tried to run it against MobileSafari running in a simulator, and it worked fine. I noticed two important things:

  • the remote server is bound to an IPv6 port, and the websocket client does not connect without a line to redefine the socket and establish the family. Not sure if it will work on a device or in a UIWebView.
  • he doesn't like spaces around separators in JSON.

Here, it looks like turning on the inspector in MobileSafari using gdb and running the script:

 $ ps x | grep MobileSafari 4968 ?? Z 0:00.00 (MobileSafari) 6234 ?? S 0:00.69 /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator5.0.sdk//Applications/MobileSafari.app/MobileSafari 6238 s007 R+ 0:00.00 grep MobileSafari $ gdb GNU gdb 6.3.50-20050815 (Apple version gdb-1708) (Thu Nov 3 21:59:02 UTC 2011) ... (gdb) attach 6234 Attaching to process 6234. Reading symbols for shared libraries . done Reading symbols for shared libraries ........................................................................................................................................................ done 0x99798c22 in mach_msg_trap () (gdb) p (void *)[WebView _enableRemoteInspector] $1 = (void *) 0x2ac93ce (gdb) detach Detaching from process 6234. (gdb) quit $ python debug.py > {"params":{"expression":"alert(\"hello from python\")"},"id":1,"method":"Runtime.evaluate"} < {"result":{"result":{"type":"undefined","description":"undefined"}},"id":1} 
+6
source

Source: https://habr.com/ru/post/904289/


All Articles