-
-
Notifications
You must be signed in to change notification settings - Fork 261
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
166 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,66 @@ | ||
import asyncio | ||
from curl_cffi import requests | ||
from contextlib import closing | ||
|
||
try: | ||
# Python 3.10+ | ||
from contextlib import aclosing | ||
except ImportError: | ||
from contextlib import asynccontextmanager | ||
|
||
@asynccontextmanager | ||
async def aclosing(thing): | ||
try: | ||
yield thing | ||
finally: | ||
await thing.aclose() | ||
|
||
|
||
URL = "https://httpbin.org/stream/20" | ||
|
||
with requests.Session() as s: | ||
r = s.get("https://httpbin.org/stream/20", stream=True) | ||
print("\n======================================================================") | ||
print("Iterating over chunks") | ||
print("=====================================================================\n") | ||
r = s.get(URL, stream=True) | ||
for chunk in r.iter_content(): | ||
print("CHUNK", chunk) | ||
r.close() | ||
|
||
|
||
with requests.Session() as s: | ||
r = s.get("https://httpbin.org/stream/20", stream=True) | ||
print("\n=====================================================================") | ||
print("Iterating on a line basis") | ||
print("=====================================================================\n") | ||
r = s.get(URL, stream=True) | ||
for line in r.iter_lines(): | ||
print("LINE", line.decode()) | ||
r.close() | ||
|
||
|
||
print("\n=====================================================================") | ||
print("Better, using closing to ensure the response is closed") | ||
print("=====================================================================\n") | ||
with closing(s.get(URL, stream=True)) as r: | ||
for chunk in r.iter_content(): | ||
print("CHUNK", chunk) | ||
|
||
|
||
async def async_examples(): | ||
async with requests.AsyncSession() as s: | ||
print("\n====================================================================") | ||
print("Using asyncio") | ||
print("====================================================================\n") | ||
r = await s.get(URL, stream=True) | ||
async for chunk in r.aiter_content(): | ||
print("CHUNK", chunk) | ||
await r.aclose() | ||
|
||
print("\n====================================================================") | ||
print("Better, using aclosing to ensure the response is closed") | ||
print("====================================================================\n") | ||
async with aclosing(await s.get(URL, stream=True)) as r: | ||
async for chunk in r.aiter_content(): | ||
print("CHUNK", chunk) | ||
|
||
|
||
asyncio.run(async_examples()) |