This repository has been archived by the owner on Dec 3, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.py
72 lines (57 loc) · 1.99 KB
/
app.py
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
66
67
68
69
70
71
72
import asyncio
import aiohttp
import requests
from flask import Flask
app = Flask(__name__)
async def fetch(url, session):
async with session.get(url) as response:
# The response read is sort of like
# a promise. If you await response,
# well, response is just a regular object,
# it doesn't have a resolution to wait for.
#
# However, I want to return the whole
# response, not just the html, so we'll
# wrap it in a dictionary.
#
# I'd like to catch exceptions here, but it doesn't
# seem to work.
html = await response.read()
return {
"resp": response,
"html": html,
"url": url,
}
@app.route("/")
def hello():
urls = [
"https://whatisjasongoldstein.com/",
"https://betheshoe.com/",
"https://theportfolioapp.com/",
"https://www.theatlantic.com/",
"https://www.example.org/",
"https://www.google.com/",
"https://badurl.whatisjasongoldstein.com/",
]
# Share the session between calls
session = aiohttp.ClientSession()
tasks = []
for url in urls:
tasks.append(asyncio.ensure_future(fetch(url, session)))
loop = asyncio.get_event_loop()
# Gather is like $.when or Promise.all in javascript
# Return exceptions tells it to add each exception to the list.
responses = loop.run_until_complete(asyncio.gather(*tasks, return_exceptions=True))
output = ""
for resp in responses:
# Return exceptions will keep the errors
# in the list instead of crashing the whole process.
if isinstance(resp, Exception):
output += "<p><b>Error:</b> %s<p>" % resp
continue
output += "<p>%s loaded with a status %s and a length of %s</p>" % (
resp["url"], resp["resp"].status, len(resp["html"])
)
return output
if __name__ == "__main__":
app.run(debug=False) # Only works without DEBUG?