ConnectionHandler
管理与浏览器的 WebSocket 连接,并路由 CDP 命令和事件。详见 远程连接。
pydoll.connection.connection_handler.ConnectionHandler
ConnectionHandler(connection_port=None, page_id=None, ws_address_resolver=get_browser_ws_address, ws_connector=connect, ws_address=None)
WebSocket connection manager for Chrome DevTools Protocol endpoints.
Handles connection lifecycle, command execution, and event subscription for both browser-level and page-level CDP endpoints.
Initialize connection handler.
| PARAMETER | DESCRIPTION |
|---|---|
connection_port
|
Browser's debugging server port.
TYPE:
|
page_id
|
Target page ID. If None, connects to browser-level endpoint.
TYPE:
|
ws_address_resolver
|
Function to resolve WebSocket URL from port.
TYPE:
|
ws_connector
|
WebSocket connection factory (mainly for testing).
TYPE:
|
ws_address
|
WebSocket address. It has priority over connection_port and page_id.
TYPE:
|
execute_command
async
Send CDP command and await response.
| PARAMETER | DESCRIPTION |
|---|---|
command
|
CDP command to send.
TYPE:
|
timeout
|
Maximum seconds to wait for response.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
T_CommandResponse
|
Parsed response object matching command's expected type. |
| RAISES | DESCRIPTION |
|---|---|
CommandFailed
|
If the browser answers with a CDP error instead of a result. |
InvalidResponse
|
If the response carries neither a result nor an error. |
CommandExecutionTimeout
|
If browser doesn't respond within timeout. |
WebSocketConnectionClosed
|
If connection closes during execution. |
execute_command_nowait
async
Send a CDP command and return as soon as it is on the wire.
The answer is still consumed when it arrives, so the pending table stays clean, and a CDP error is logged instead of raised. Meant for streams of input events whose order the socket already guarantees: awaiting each answer would add a network round trip between consecutive mouse moves, which no real input device has.
| PARAMETER | DESCRIPTION |
|---|---|
command
|
CDP command to send.
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
WebSocketConnectionClosed
|
If the connection cannot be (re)established. |
execute_commands
async
Send several commands back to back and wait for all of their answers.
The browser handles the commands of one session in the order they arrive, so sending a sequence without waiting for each answer keeps the ordering (keydown before keyup, for example) while paying one round trip for the whole batch instead of one per command.
| PARAMETER | DESCRIPTION |
|---|---|
commands
|
Commands to send, in order.
TYPE:
|
timeout
|
Seconds to wait for the last answer.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list[T_CommandResponse]
|
The responses, in the same order as |
| RAISES | DESCRIPTION |
|---|---|
CommandFailed
|
If the browser rejects any command in the batch. |
CommandExecutionTimeout
|
If the answers do not all arrive in time. |
WebSocketConnectionClosed
|
If the connection closes meanwhile. |
register_callback
async
Register event listener for CDP events.
| PARAMETER | DESCRIPTION |
|---|---|
event_name
|
CDP event name (e.g., 'Page.loadEventFired').
TYPE:
|
callback
|
Async function called when event occurs.
TYPE:
|
temporary
|
If True, callback removed after first trigger.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
int
|
Callback ID for later removal. |
Note
Corresponding CDP domain must be enabled before events fire.