Skip to navigation
Create a simple middleware for aiohttp web
29.11.21
@web.middleware async def couchdb_logs(request, handler): print("xxxx") return await handler(request) def main(argv): logging.basicConfig(level=logging.DEBUG) loop = asyncio.get_event_loop() app = init(loop, argv) app.middlewares.append(couchdb_logs) web.run_app(app, host=app['config']['host'], port=app['config']['port']) if __name__ == '__main__': main(sys.argv[1:])
http://demos.aiohttp.org/en/latest/tutorial.html
Reply
Anonymous
Add a usage logger to the python API: @web.middleware async def couchdb_logs(request, handler): if request.path_qs not in ['/stylesheet.css','/favicon.ico'] : data = { "_id": "{0}_sapi{1}".format(int(time.time()),random.randint(10,99)), 'loggin': 'SAPI', 'IP': request.remote, 'page': str(request.url), "log_api": "https://couchdb.foo-bar.net/logger", 'unixtime': int(time.time()), 'date': datetime.datetime.fromtimestamp(int(time.time())).strftime('%Y%m%d%H%M'), 'fingerprint': '', 'timezone': '-7', } for i in request.headers: data[i] = request.headers[i] url = "http://couchdb.bar-foo.net" try: server = couchdb2.Server(href=url,username='bar',password='foo',use_session=False,ca_file=None) db = server.get("logger") db.put(data) except Exception as e: print(e) return await handler(request) def main(argv): logging.basicConfig(level=logging.DEBUG) loop = asyncio.get_event_loop() app = init(loop, argv) app.middlewares.append(couchdb_logs) web.run_app(app, host=app['config']['host'], port=app['config']['port']) if __name__ == '__main__': main(sys.argv[1:])
29.11.21
Reply
Anonymous
Information Epoch 1745690457
Distrust all claims for one true way.
Home
Notebook
Contact us