- Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlesson07_check_asynchronous.py
More file actions
Latest commit
65 lines (61 loc) · 2.04 KB
/
Copy pathlesson07_check_asynchronous.py
File metadata and controls
65 lines (61 loc) · 2.04 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# SuperFastPython.com
# example checking the status of sites asynchronously
fromurllib.requestimporturlopen
fromurllib.errorimportURLError
fromurllib.errorimportHTTPError
fromhttpimportHTTPStatus
fromconcurrent.futuresimportThreadPoolExecutor
fromconcurrent.futuresimportas_completed
# get the status of a website
defget_website_status(url):
# handle connection errors
try:
# open a connection to the server with a timeout
withurlopen(url, timeout=3) asconnection:
# get the response code, e.g. 200
code=connection.getcode()
returncode
exceptHTTPErrorase:
returne.code
exceptURLErrorase:
returne.reason
exceptExceptionase:
returne
# interpret an HTTP response code into a status
defget_status(code):
ifcode==HTTPStatus.OK:
return'OK'
return'ERROR'
# check status of a list of websites
defcheck_status_urls(urls):
# create the thread pool
withThreadPoolExecutor(len(urls)) asexe:
# issue each task, map of futures to urls
future_to_url= {
exe.submit(get_website_status, url):url
forurlinurls}
# get results as they are available
forfutureinas_completed(future_to_url):
# get the url for the future
url=future_to_url[future]
# get the status for the website
code=future.result()
# interpret the status
status=get_status(code)
# report status
print(f'{url:20s}\t{status:5s}\t{code}')
# protect the entry point
if__name__=='__main__':
# list of urls to check
urls= ['https://twitter.com',
'https://google.com',
'https://facebook.com',
'https://reddit.com',
'https://youtube.com',
'https://amazon.com',
'https://wikipedia.org',
'https://ebay.com',
'https://instagram.com',
'https://cnn.com']
# check all urls
check_status_urls(urls)