Skip to content

Commit a29c541

Browse files
committed
dev(script): add a minimal script to try out Caching Control
1 parent 89cd662 commit a29c541

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

tests/dev/dev_cached_http.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import http.client
2+
import logging
3+
from pathlib import Path
4+
5+
import requests
6+
from cachecontrol import CacheControl
7+
from cachecontrol.caches.file_cache import FileCache
8+
9+
http.client.HTTPConnection.debuglevel = 1
10+
logging.basicConfig()
11+
logging.getLogger().setLevel(logging.DEBUG)
12+
req_log = logging.getLogger("requests.packages.urllib3")
13+
req_log.setLevel(logging.DEBUG)
14+
req_log.propagate = True
15+
16+
17+
sess = CacheControl(
18+
requests.Session(), cache=FileCache(".web_cache"), cacheable_methods=("HEAD", "GET")
19+
)
20+
21+
22+
# get requests
23+
resp = sess.get("https://geotribu.fr")
24+
resp_img = sess.get(
25+
"https://cdn.geotribu.fr/img/articles-blog-rdp/capture-ecran/kevish_Air-Traffic.png"
26+
)
27+
28+
# try again, cache hit expected
29+
resp = sess.get("https://geotribu.fr")
30+
resp_img = sess.get(
31+
"https://cdn.geotribu.fr/img/articles-blog-rdp/capture-ecran/kevish_Air-Traffic.png"
32+
)
33+
34+
# head requests
35+
resp_img = sess.head(
36+
"https://cdn.geotribu.fr/img/articles-blog-rdp/capture-ecran/kevish_Air-Traffic.png"
37+
)
38+
39+
40+
# try again, cache hit expected
41+
resp_img = sess.head(
42+
"https://cdn.geotribu.fr/img/articles-blog-rdp/capture-ecran/kevish_Air-Traffic.png"
43+
)
44+
45+
print(list(Path(".web_cache").iterdir()))

0 commit comments

Comments
 (0)