Browse Source

poc before

Bastien Sevajol 5 years ago
parent
commit
b5e9ebe7b6
1 changed files with 52 additions and 0 deletions
  1. 52 0
      poc.py

+ 52 - 0
poc.py View File

@@ -0,0 +1,52 @@
1
+import asyncio
2
+import aiohttp
3
+import json
4
+from aiohttp import web
5
+
6
+
7
+async def uptime_handler(request):
8
+    resp = web.StreamResponse(
9
+        status=200,
10
+        reason='OK',
11
+        headers={
12
+            'Content-Type': 'text/csv',
13
+            'Content-Disposition': 'attachment; filename="filename.csv"',
14
+        }
15
+    )
16
+    await resp.prepare(request)
17
+
18
+    try:
19
+        async with aiohttp.ClientSession(loop=loop) as session:
20
+            url = 'http://localhost:8086/query?chunk_size=1000&chunked=true&db=resourceAux&q=SELECT+%2A+FROM+resource_aux'  # nopep8
21
+            async with session.get(url) as response:
22
+                async for chunk in response.content:
23
+                    bytes_to_str = chunk.decode('utf-8')
24
+                    result = json.loads(bytes_to_str)['results'][0]['series'][0]['values']  # nopep8
25
+                    for r in result:
26
+                        await resp.write(str.encode(str(r)+'\n'))
27
+
28
+    except Exception as e:
29
+        # So you can observe on disconnects and such.
30
+        print(repr(e))
31
+        raise
32
+
33
+    return resp
34
+
35
+
36
+async def build_server(loop, address, port):
37
+    app = web.Application(loop=loop)
38
+    app.router.add_route('GET', "/uptime", uptime_handler)
39
+
40
+    return await loop.create_server(app.make_handler(), address, port)
41
+
42
+
43
+if __name__ == '__main__':
44
+    loop = asyncio.get_event_loop()
45
+    loop.run_until_complete(build_server(loop, 'localhost', 9999))
46
+    print("Server ready!")
47
+
48
+    try:
49
+        loop.run_forever()
50
+    except KeyboardInterrupt:
51
+        print("Shutting Down!")
52
+        loop.close()