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()
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}
source share