1from concurrent.futures import ThreadPoolExecutor as PoolExecutor
2import http.client
3import socket
4
5def get_it(url):
6 try:
7 # always set a timeout when you connect to an external server
8 connection = http.client.HTTPSConnection(url, timeout=2)
9
10 connection.request("GET", "/")
11
12 response = connection.getresponse()
13
14 return response.read()
15 except socket.timeout:
16 # in a real world scenario you would probably do stuff if the
17 # socket goes into timeout
18 pass
19
20urls = [
21 "www.google.com",
22 "www.youtube.com",
23 "www.wikipedia.org",
24 "www.reddit.com",
25 "www.httpbin.org"
26] * 200
27
28with PoolExecutor(max_workers=4) as executor:
29 for _ in executor.map(get_it, urls):
30 pass
31