-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathwebservice_animals_aiottp.py
More file actions
43 lines (33 loc) · 896 Bytes
/
webservice_animals_aiottp.py
File metadata and controls
43 lines (33 loc) · 896 Bytes
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
from asyncio import sleep
from aiohttp import web
FARM = {
'cow': 'Moo!',
'pig': 'Oink!',
'sheep': 'Baaa!',
'chicken': 'Cluck!',
'bird': 'Tweet!',
'duck': 'Quack!',
'dog': 'Woof!',
'cat': 'Meow!',
'frog': 'Ribbit!',
'horse': 'Neigh!',
'turkey': 'Gobble-Gobble!',
'rooster': 'Cock-a-Doodle-Doo!'
}
async def hello(request):
msg = 'Welcome to the farm!'
return web.Response(text=msg)
async def speak(request):
animal = request.match_info['name']
if animal not in FARM:
return web.Response(
text='The animal {0} was not found.'.format(animal),
status=404
)
await sleep(5)
return web.Response(text=FARM[animal])
app = web.Application()
app.router.add_get('/animals', hello)
resource = app.router.add_resource('/animals/{name}')
resource.add_route('GET', speak)
web.run_app(app)