Skip to content

Instantly share code, notes, and snippets.

@koi8-r
Last active May 15, 2020 23:37
Show Gist options
  • Save koi8-r/59f0d170414e7c86b56444ab7c9264e3 to your computer and use it in GitHub Desktop.
Save koi8-r/59f0d170414e7c86b56444ab7c9264e3 to your computer and use it in GitHub Desktop.
from aiohttp import web, ClientSession as UA
import asyncio as A
import sys, os
def update(data: bytes):
path = __file__
with open(path, mode='wb') as f:
f.write(data)
print('Updated')
print(sys.executable)
print(sys.argv)
os.execl(sys.executable, *sys.argv)
async def update_svc(_app):
while True:
await A.sleep(1)
async with UA() as ua:
async with ua.get('https://gist.githubusercontent.com/koi8-r/59f0d170414e7c86b56444ab7c9264e3/raw/a19447a8451c2974626ecb819b8c50389b53e196/pst-updater.py') as resp:
script: bytes = await resp.read()
update(script)
async def start_background_tasks(app):
app['update_svc'] = A.create_task(update_svc(app))
async def stop_background_tasks(app):
app['update_svc'].cancel()
await app['update_svc']
async def index(req: web.Request):
return web.Response(text='Home')
app = web.Application()
app.on_startup.append(start_background_tasks)
app.on_cleanup.append(stop_background_tasks)
app.get('/', index)
web.run_app(app)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment