CDP-Socket
- Handle Chrome-Developer-Protocol connections
Feel free to test my code!
Getting Started
Dependencies
- Python >= 3.7
- Chrome-Browser installed
Installing
- [Windows] Install Chrome-Browser
pip install cdp-socket
Single socket
from cdp_socket.utils.utils import launch_chrome, random_port
from cdp_socket.utils.conn import get_websock_url
from cdp_socket.socket import SingleCDPSocket
import os
import asyncio
async def main():
data_dir = os.getcwd()+"/data_dir"
PORT = random_port()
process = launch_chrome(data_dir,PORT)
websock_url = await get_websock_url(PORT, timeout=5)
async with SingleCDPSocket(websock_url, timeout=5) as sock:
targets = await sock.exec("Target.getTargets")
print(targets)
os.kill(process.pid, 15)
asyncio.run(main())
on_closed callback
from cdp_socket.socket import SingleCDPSocket
def on_closed(code, reason):
print("Closed with: ", code, reason)
async with SingleCDPSocket(websock_url, timeout=5) as sock:
sock.on_closed.append(on_closed)
# close window for dispatching this event
targets = await sock.exec("Target.getTargets")
print(targets)
add event listener
from cdp_socket.socket import SingleCDPSocket
import asyncio
def on_detached(params):
print("Detached with: ", params)
async with SingleCDPSocket(websock_url, timeout=5) as sock:
# close window for dispatching this event
sock.add_listener('Inspector.detached', on_detached)
await asyncio.sleep(1000)
iterate over event
from cdp_socket.socket import SingleCDPSocket
async with SingleCDPSocket(websock_url, timeout=5) as sock:
async for i in sock.method_iterator('Inspector.detached'):
print(i)
break
wait for event
from cdp_socket.socket import SingleCDPSocket
async with SingleCDPSocket(websock_url, timeout=5) as sock:
res = await sock.wait_for('Inspector.detached')
print(res)
synchronous
from cdp_socket.utils.utils import launch_chrome, random_port
from cdp_socket.utils.conn import get_websock_url
from cdp_socket.socket import SingleCDPSocket
import os
import shutil
import asyncio
data_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)
CDPSocket
from cdp_socket.utils.utils import launch_chrome, random_port
from cdp_socket.socket import CDPSocket
import os
import asyncio
async def main():
data_dir = os.getcwd()+"/data_dir"
PORT = random_port()
process = launch_chrome(data_dir,PORT)
async with CDPSocket(PORT) as base_socket:
targets = await base_socket.targets
sock1 = await base_socket.get_socket(targets[0])
targets = await sock1.exec("Target.getTargets")
print(targets)
os.kill(process.pid, 15)
asyncio.run(main())
Custom exception handling
You can implement custom exception handling as following
import cdp_socket
import sys
# print exception without traceback
sys.modules["cdp_socket"].EXC_HANDLER = lambda e: print(f'Exception in event-handler:\n{e.__class__.__module__}.{e.__class__.__name__}: {e}', file=sys.stderr)
Help
Please feel free to open an issue or fork!
Performance
On a Win10 Laptop
executing "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 javascript
JS benchmark:
passed 22_990.0 mB in 3.16 s, 7_284.5374 mB/sec
1_584 (each 4.598 mB) exec (JS) per s
Authors
Disclaimer
I am not responsible what you use the code for!!! Also no warranty!
Acknowledgments
Inspiration, code snippets, etc.
contributors
- thanks to @Redrrx who gave me some starting-points
Metadata
Release files for cdp-socket 1.2.8
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| cdp-socket-1.2.8.tar.gz | 19.8 kB | Without provenance | Details for cdp-socket-1.2.8.tar.gz |
Release files / cdp-socket-1.2.8.tar.gz
| Download URL | cdp-socket-1.2.8.tar.gz |
|---|---|
| Size | 19.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
d8a3d55883205c7c45c05292cf5ef5a5c74534873e369e258e61213cce15be1a
|
|
BLAKE2b-256 checksum How to use checksums |
7d2858812797e54fb8cf22bff61125e5a7d2763de1a86855549ecc417bdd06d5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.10.11
|
Security for cdp-socket 1.2.8
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.