aiopoc.py 589B

1234567891011121314151617181920212223242526272829303132
  1. from sh import tail
  2. from asyncio import sleep
  3. from aiohttp import web
  4. async def handle(request):
  5. response = web.StreamResponse(
  6. status=200,
  7. reason='OK',
  8. headers={
  9. 'Content-Type': 'text/plain; charset=utf-8',
  10. },
  11. )
  12. await response.prepare(request)
  13. response.enable_chunked_encoding()
  14. for line in tail("-f", "aiopocdata.txt", _iter=True):
  15. await response.write(line.encode('utf-8'))
  16. await sleep(0.1)
  17. return response
  18. app = web.Application()
  19. app.add_routes([
  20. web.get('/', handle)
  21. ])
  22. web.run_app(app)