blob: 1a2decc643ed43ee842b71dccd59b23d2bc5c8a3 (
plain)
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
|
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import time
import requests
from requests import async
import requests_cache
requests_cache.configure('example_cache')
def main():
# Once cached, delayed page will be taken from cache
# redirects also handled
for i in range(5):
requests.get('http://httpbin.org/delay/2')
r = requests.get('http://httpbin.org/redirect/5')
print(r.text)
# What about async? It's also supported!
rs = [async.get('http://httpbin.org/delay/%s' % i) for i in range(5)]
for r in async.map(rs):
print(r.text)
# And if we need to get fresh page or don't want to cache it?
with requests_cache.disabled():
print(requests.get('http://httpbin.org/ip').text)
# Debugging info about cache
print(requests_cache.get_cache())
if __name__ == "__main__":
t = time.time()
main()
print('Elapsed: %.3f seconds' % (time.time() - t))
|