import asyncio
import json

import aiohttp

API = {"jsonrpc": "2.0", "method": "apiinfo.version", "params": {}, "id": 1}
TARGETS = [
    "https://demo-zabbix.racom.eu/zabbix/api_jsonrpc.php",
    "https://demo-zabbix.racom.eu/zabbix/api_jsonrpc.php",
    "http://45.223.168.177:443/api_jsonrpc.php",
]


async def api_version(sess, url):
    try:
        async with sess.post(url, json=API, ssl=False, allow_redirects=False) as r:
            if r.status != 200:
                return "status=%d" % r.status
            raw = await r.content.read(600)
        j = json.loads(raw)
        v = j.get("result")
        return v if isinstance(v, str) and v[:1].isdigit() else "no-result:%s" % raw[:80]
    except Exception as e:
        return "err:%s" % type(e).__name__


async def main():
    to = aiohttp.ClientTimeout(total=6, connect=3, sock_read=5)
    async with aiohttp.ClientSession(timeout=to,
                                     connector=aiohttp.TCPConnector(limit=0, ssl=False)) as s:
        for u in TARGETS:
            print(u, "->", await api_version(s, u))


asyncio.run(main())
