-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathstreaming.py
More file actions
37 lines (26 loc) · 1.17 KB
/
Copy pathstreaming.py
File metadata and controls
37 lines (26 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
"""Streaming examples for primp."""
import asyncio
import primp
client = primp.Client(impersonate="chrome_146")
# Sync: context manager streaming
with client.get("https://httpbin.org/stream/5", stream=True) as resp:
for chunk in resp.iter_bytes():
print(f"Chunk: {len(chunk)} bytes")
# Sync: line streaming
with client.get("https://httpbin.org/stream/3", stream=True) as resp:
for line in resp.iter_lines():
print(f"Line: {line}")
# Sync: text streaming
with client.get("https://httpbin.org/stream/3", stream=True) as resp:
for text_chunk in resp.iter_text():
print(f"Text chunk: {text_chunk[:50]}...")
async def async_streaming():
async with primp.AsyncClient(impersonate="chrome_146") as client:
async with await client.get("https://httpbin.org/stream/5", stream=True) as resp:
async for chunk in resp.aiter_bytes():
print(f"Async chunk: {len(chunk)} bytes")
async with await client.get("https://httpbin.org/stream/3", stream=True) as resp:
async for line in resp.aiter_lines():
print(f"Async line: {line}")
if __name__ == "__main__":
asyncio.run(async_streaming())