Package: cdp-socket
CDP-SocketHandleChrome-Developer-ProtocolconnectionsFeel free to test my code!Getting StartedDependenciesPython >= 3.7Chrome-BrowserinstalledInstalling[Windows] InstallChrome-Browserpip install cdp-socketSingle socketfromcdp_socket.utils.utilsimportlaunch_chrome,random_portfromcdp_socket.utils.connimportget_websock_urlfromcdp_socket.socketimportSingleCDPSocketimportosimportasyncioasyncdefmain():data_dir=os.getcwd()+"/data_dir"PORT=random_port()process=launch_chrome(data_dir,PORT)websock_url=awaitget_websock_url(PORT,timeout=5)asyncwithSingleCDPSocket(websock_url,timeout=5)assock:targets=awaitsock.exec("Target.getTargets")print(targets)os.kill(process.pid,15)asyncio.run(main())on_closed callbackfromcdp_socket.socketimportSingleCDPSocketdefon_closed(code,reason):print("Closed with: ",code,reason)asyncwithSingleCDPSocket(websock_url,timeout=5)assock:sock.on_closed.append(on_closed)# close window for dispatching this eventtargets=awaitsock.exec("Target.getTargets")print(targets)add event listenerfromcdp_socket.socketimportSingleCDPSocketimportasynciodefon_detached(params):print("Detached with: ",params)asyncwithSingleCDPSocket(websock_url,timeout=5)assock:# close window for dispatching this eventsock.add_listener('Inspector.detached',on_detached)awaitasyncio.sleep(1000)iterate over eventfromcdp_socket.socketimportSingleCDPSocketasyncwithSingleCDPSocket(websock_url,timeout=5)assock:asyncforiinsock.method_iterator('Inspector.detached'):print(i)breakwait for eventfromcdp_socket.socketimportSingleCDPSocketasyncwithSingleCDPSocket(websock_url,timeout=5)assock:res=awaitsock.wait_for('Inspector.detached')print(res)synchronousfromcdp_socket.utils.utilsimportlaunch_chrome,random_portfromcdp_socket.utils.connimportget_websock_urlfromcdp_socket.socketimportSingleCDPSocketimportosimportshutilimportasynciodata_dir=os.getcwd()+"/data_dir"PORT=random_port()process=launch_chrome(data_dir,PORT)loop=asyncio.get_event_loop()websock_url=loop.run_until_complete(get_websock_url(PORT,timeout=5))conn=loop.run_until_complete(SingleCDPSocket(websock_url,timeout=5,loop=loop))targets=loop.run_until_complete(conn.exec("Target.getTargets"))print(targets)os.kill(process.pid,15)shutil.rmtree(data_dir)CDPSocketfromcdp_socket.utils.utilsimportlaunch_chrome,random_portfromcdp_socket.socketimportCDPSocketimportosimportasyncioasyncdefmain():data_dir=os.getcwd()+"/data_dir"PORT=random_port()process=launch_chrome(data_dir,PORT)asyncwithCDPSocket(PORT)asbase_socket:targets=awaitbase_socket.targetssock1=awaitbase_socket.get_socket(targets[0])targets=awaitsock1.exec("Target.getTargets")print(targets)os.kill(process.pid,15)asyncio.run(main())Custom exception handlingYou can implement custom exception handling as followingimportcdp_socketimportsys# print exception without tracebacksys.modules["cdp_socket"].EXC_HANDLER=lambdae:print(f'Exception in event-handler:\n{e.__class__.__module__}.{e.__class__.__name__}:{e}',file=sys.stderr)HelpPlease feel free to open an issue or fork!PerformanceOn a Win10 Laptopexecuting"Browser.getVersion"Static benchmark:
passed 3_640.0 mB  in 2.74 s, 13_308.958 mB/sec 
3_656 (each 364 bytes) exec per sec.for returning a big (static) object over javascriptJS benchmark:
passed 22_990.0 mB in 3.16 s, 7_284.5374 mB/sec 
1_584 (each 4.598 mB) exec (JS) per sAuthorsAurin AegerterDisclaimerI am not responsible what you use the code for!!! Also no warranty!AcknowledgmentsInspiration, code snippets, etc.Chrome-Developer-Protocolcontributorsthanks to@Redrrxwho gave me some starting-points