GithubHelp home page GithubHelp logo

fredgido / cdp-socket Goto Github PK

View Code? Open in Web Editor NEW

This project forked from kaliiiiiiiiii/cdp-socket

0.0 0.0 0.0 35 KB

socket for handling chrome-developer-protocol connections

License: Other

Python 100.00%

cdp-socket's Introduction

CDP-Socket

Feel free to test my code!

Getting Started

Dependencies

Installing

CDPSocket

from cdp_socket.utils.utils import launch_chrome, random_port
from cdp_socket.socket import CDPSocket

import os
import asyncio

async def main():
    PORT = random_port()
    process = launch_chrome(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())

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():
    PORT = random_port()
    process = launch_chrome(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 asyncio

PORT = random_port()
process = launch_chrome(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)

Help

Please feel free to open an issue or fork!

Todo

Deprecated

Authors

Aurin Aegerter

License

Shield: CC BY-NC-SA 4.0

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

CC BY-NC-SA 4.0

known bugs

  • timeout doesn't raise in httpx (bug)

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.