|
1 | | -"""Example: direct-to-VM browser routing for process exec and raw HTTP.""" |
2 | | - |
3 | | -from typing import Any, cast |
| 1 | +"""Example: direct-to-VM browser routing for raw HTTP.""" |
4 | 2 |
|
5 | 3 | import httpx |
6 | 4 |
|
7 | 5 | from kernel import Kernel |
8 | 6 |
|
9 | 7 |
|
10 | 8 | def main() -> None: |
11 | | - with Kernel() as client: |
12 | | - browsers = cast(Any, client.browsers) |
13 | | - browser = browsers.create(headless=True) |
14 | | - try: |
15 | | - response = cast(httpx.Response, browsers.request(browser.session_id, "GET", "https://example.com")) |
16 | | - print("status", response.status_code) |
17 | | - |
18 | | - with browsers.stream(browser.session_id, "GET", "https://example.com") as streamed: |
19 | | - print("streamed-bytes", len(streamed.read())) |
20 | | - finally: |
21 | | - browsers.delete_by_id(browser.session_id) |
| 9 | + client = Kernel() |
| 10 | + |
| 11 | + browser = client.browsers.create() |
| 12 | + |
| 13 | + # Raw browser curl: streams the response. Use for large responses, when you want to stream, |
| 14 | + # or when you want httpx.Response semantics. |
| 15 | + response: httpx.Response = client.browsers.request(browser.session_id, "GET", "https://example.com") |
| 16 | + print("status", response.status_code) |
| 17 | + |
| 18 | + # Buffered browser curl: returns the full response in a JSON envelope. Use for small responses. |
| 19 | + buffered = client.browsers.curl(browser.session_id, url="https://example.com", method="GET") |
| 20 | + print("body", buffered.body) |
| 21 | + |
| 22 | + client.browsers.delete_by_id(browser.session_id) |
22 | 23 |
|
23 | 24 |
|
24 | 25 | if __name__ == "__main__": |
|
0 commit comments