Skip to content

Commit b01608f

Browse files
authored
add httpx for dec 2020 (#71)
1 parent c570582 commit b01608f

File tree

1 file changed

+374
-0
lines changed

1 file changed

+374
-0
lines changed

HTTPX/HTTPX.ipynb

Lines changed: 374 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,374 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {
6+
"slideshow": {
7+
"slide_type": "slide"
8+
}
9+
},
10+
"source": [
11+
"# HTTPX\n",
12+
"\n",
13+
"<img src=\"https://raw.githubusercontent.com/encode/httpx/master/docs/img/butterfly.png\" alt=\"HTTPX Logo\" style=\"width: 400px;\"/>\n",
14+
"\n",
15+
"https://www.python-httpx.org/\n",
16+
"\n",
17+
"*A next-generation HTTP client for Python.*\n",
18+
"\n",
19+
"HTTPX is a fully featured HTTP client for Python 3, which provides sync and async APIs, and support for both HTTP/1.1 and HTTP/2.\n",
20+
"\n",
21+
"Alternatives: `aiohttp`, `asks`"
22+
]
23+
},
24+
{
25+
"cell_type": "code",
26+
"execution_count": 1,
27+
"metadata": {
28+
"slideshow": {
29+
"slide_type": "slide"
30+
}
31+
},
32+
"outputs": [],
33+
"source": [
34+
"from datetime import datetime"
35+
]
36+
},
37+
{
38+
"cell_type": "markdown",
39+
"metadata": {
40+
"slideshow": {
41+
"slide_type": "fragment"
42+
}
43+
},
44+
"source": [
45+
"Using httpbin.org:\n",
46+
"\n",
47+
"`https://httpbin.org/delay/1`"
48+
]
49+
},
50+
{
51+
"cell_type": "code",
52+
"execution_count": null,
53+
"metadata": {
54+
"slideshow": {
55+
"slide_type": "slide"
56+
}
57+
},
58+
"outputs": [],
59+
"source": [
60+
"import requests"
61+
]
62+
},
63+
{
64+
"cell_type": "code",
65+
"execution_count": null,
66+
"metadata": {
67+
"slideshow": {
68+
"slide_type": "fragment"
69+
}
70+
},
71+
"outputs": [],
72+
"source": [
73+
"def requests_get(index=None):\n",
74+
" response = requests.get(\"https://httpbin.org/delay/1\")\n",
75+
" response.raise_for_status()\n",
76+
" print(f\"{index} - {response.status_code} - {response.elapsed}\")"
77+
]
78+
},
79+
{
80+
"cell_type": "code",
81+
"execution_count": null,
82+
"metadata": {
83+
"slideshow": {
84+
"slide_type": "fragment"
85+
}
86+
},
87+
"outputs": [],
88+
"source": [
89+
"requests_get()"
90+
]
91+
},
92+
{
93+
"cell_type": "code",
94+
"execution_count": null,
95+
"metadata": {
96+
"slideshow": {
97+
"slide_type": "subslide"
98+
}
99+
},
100+
"outputs": [],
101+
"source": [
102+
"before = datetime.now()\n",
103+
"\n",
104+
"for index in range(0, 5):\n",
105+
" requests_get(index)\n",
106+
" \n",
107+
"after = datetime.now()\n",
108+
"print(f\"total time: {after - before}\")"
109+
]
110+
},
111+
{
112+
"cell_type": "code",
113+
"execution_count": null,
114+
"metadata": {
115+
"slideshow": {
116+
"slide_type": "slide"
117+
}
118+
},
119+
"outputs": [],
120+
"source": [
121+
"import httpx"
122+
]
123+
},
124+
{
125+
"cell_type": "code",
126+
"execution_count": null,
127+
"metadata": {
128+
"slideshow": {
129+
"slide_type": "fragment"
130+
}
131+
},
132+
"outputs": [],
133+
"source": [
134+
"def httpx_get(index=None):\n",
135+
" response = httpx.get(\"https://httpbin.org/delay/1\")\n",
136+
" response.raise_for_status()\n",
137+
" print(f\"{index} - {response.status_code} - {response.elapsed}\")"
138+
]
139+
},
140+
{
141+
"cell_type": "code",
142+
"execution_count": null,
143+
"metadata": {
144+
"slideshow": {
145+
"slide_type": "fragment"
146+
}
147+
},
148+
"outputs": [],
149+
"source": [
150+
"httpx_get()"
151+
]
152+
},
153+
{
154+
"cell_type": "code",
155+
"execution_count": null,
156+
"metadata": {
157+
"slideshow": {
158+
"slide_type": "subslide"
159+
}
160+
},
161+
"outputs": [],
162+
"source": [
163+
"before = datetime.now()\n",
164+
"\n",
165+
"for index in range(0, 5):\n",
166+
" httpx_get(index)\n",
167+
" \n",
168+
"after = datetime.now()\n",
169+
"print(f\"total time: {after - before}\")"
170+
]
171+
},
172+
{
173+
"cell_type": "code",
174+
"execution_count": null,
175+
"metadata": {
176+
"slideshow": {
177+
"slide_type": "slide"
178+
}
179+
},
180+
"outputs": [],
181+
"source": [
182+
"async with httpx.AsyncClient() as client:\n",
183+
" response = await client.get('https://httpbin.org/delay/1')\n",
184+
"print(response)"
185+
]
186+
},
187+
{
188+
"cell_type": "code",
189+
"execution_count": null,
190+
"metadata": {
191+
"slideshow": {
192+
"slide_type": "fragment"
193+
}
194+
},
195+
"outputs": [],
196+
"source": [
197+
"async def async_httpx_get(index=None):\n",
198+
" async with httpx.AsyncClient() as client:\n",
199+
" response = await client.get(\"https://httpbin.org/delay/1\")\n",
200+
" response.raise_for_status()\n",
201+
" print(f\"{index} - {response.status_code} - {response.elapsed}\")"
202+
]
203+
},
204+
{
205+
"cell_type": "code",
206+
"execution_count": null,
207+
"metadata": {
208+
"slideshow": {
209+
"slide_type": "fragment"
210+
}
211+
},
212+
"outputs": [],
213+
"source": [
214+
"await async_httpx_get()"
215+
]
216+
},
217+
{
218+
"cell_type": "code",
219+
"execution_count": null,
220+
"metadata": {
221+
"slideshow": {
222+
"slide_type": "subslide"
223+
}
224+
},
225+
"outputs": [],
226+
"source": [
227+
"before = datetime.now()\n",
228+
"\n",
229+
"for index in range(0, 5):\n",
230+
" await async_httpx_get(index)\n",
231+
" \n",
232+
"after = datetime.now()\n",
233+
"print(f\"total time: {after - before}\")"
234+
]
235+
},
236+
{
237+
"cell_type": "code",
238+
"execution_count": null,
239+
"metadata": {
240+
"slideshow": {
241+
"slide_type": "subslide"
242+
}
243+
},
244+
"outputs": [],
245+
"source": [
246+
"many_gets = tuple(async_httpx_get(index) for index in range(0,5))"
247+
]
248+
},
249+
{
250+
"cell_type": "code",
251+
"execution_count": null,
252+
"metadata": {
253+
"slideshow": {
254+
"slide_type": "fragment"
255+
}
256+
},
257+
"outputs": [],
258+
"source": [
259+
"import asyncio\n",
260+
"\n",
261+
"before = datetime.now()\n",
262+
"\n",
263+
"await asyncio.gather(*many_gets)\n",
264+
"\n",
265+
"after = datetime.now()\n",
266+
"print(f\"total time: {after - before}\")"
267+
]
268+
},
269+
{
270+
"cell_type": "code",
271+
"execution_count": null,
272+
"metadata": {
273+
"slideshow": {
274+
"slide_type": "slide"
275+
}
276+
},
277+
"outputs": [],
278+
"source": [
279+
"semaphore = asyncio.Semaphore(3)\n",
280+
"\n",
281+
"async def async_semaphore_httpx_get(index=None):\n",
282+
" async with semaphore:\n",
283+
" async with httpx.AsyncClient() as client:\n",
284+
" response = await client.get(\"https://httpbin.org/delay/1\")\n",
285+
" response.raise_for_status()\n",
286+
" print(f\"{index} - {response.status_code} - {response.elapsed}\")"
287+
]
288+
},
289+
{
290+
"cell_type": "code",
291+
"execution_count": null,
292+
"metadata": {
293+
"slideshow": {
294+
"slide_type": "subslide"
295+
}
296+
},
297+
"outputs": [],
298+
"source": [
299+
"semaphore_many_gets = tuple(\n",
300+
" async_semaphore_httpx_get(index) for index in range(0,10))"
301+
]
302+
},
303+
{
304+
"cell_type": "code",
305+
"execution_count": null,
306+
"metadata": {
307+
"slideshow": {
308+
"slide_type": "fragment"
309+
}
310+
},
311+
"outputs": [],
312+
"source": [
313+
"before = datetime.now()\n",
314+
"\n",
315+
"await asyncio.gather(*semaphore_many_gets)\n",
316+
"\n",
317+
"after = datetime.now()\n",
318+
"print(f\"total time: {after - before}\")"
319+
]
320+
},
321+
{
322+
"cell_type": "markdown",
323+
"metadata": {
324+
"slideshow": {
325+
"slide_type": "slide"
326+
}
327+
},
328+
"source": [
329+
"## Features\n",
330+
"\n",
331+
"HTTPX is a high performance asynchronous HTTP client, that builds on the well-established usability of requests, and gives you:\n",
332+
"\n",
333+
"- A broadly requests-compatible API.\n",
334+
"- Standard synchronous interface, but with async support if you need it.\n",
335+
"- HTTP/1.1 and HTTP/2 support.\n",
336+
"- Ability to make requests directly to WSGI applications or ASGI applications.\n",
337+
"- Strict timeouts everywhere.\n",
338+
"- Fully type annotated.\n",
339+
"- 100% test coverage.\n",
340+
"\n",
341+
"Plus all the standard features of requests..."
342+
]
343+
},
344+
{
345+
"cell_type": "code",
346+
"execution_count": null,
347+
"metadata": {},
348+
"outputs": [],
349+
"source": []
350+
}
351+
],
352+
"metadata": {
353+
"celltoolbar": "Slideshow",
354+
"kernelspec": {
355+
"display_name": "Python 3",
356+
"language": "python",
357+
"name": "python3"
358+
},
359+
"language_info": {
360+
"codemirror_mode": {
361+
"name": "ipython",
362+
"version": 3
363+
},
364+
"file_extension": ".py",
365+
"mimetype": "text/x-python",
366+
"name": "python",
367+
"nbconvert_exporter": "python",
368+
"pygments_lexer": "ipython3",
369+
"version": "3.8.5"
370+
}
371+
},
372+
"nbformat": 4,
373+
"nbformat_minor": 4
374+
}

0 commit comments

Comments
 (0)