summaryrefslogtreecommitdiffhomepage
path: root/scripts/test-url
blob: 26cf172e7e1b36cc1bac6e23ee0a84fa967cb94d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/usr/bin/env python3
import asyncio
import httpx
import sys
from urllib.parse import urlparse
import socket

SUCCESS_CODES = {200, 403, 404, 405}

async def check_url(client, url):
    url = url.strip()
    if not url:
        return None
    if not url.startswith(('http://', 'https://')):
        url = 'https://' + url

    host = urlparse(url).hostname
    try:
        ip = socket.gethostbyname(host)
    except Exception as e:
        ip = str(e)
    try:
        response = await client.head(url, timeout=5.0, follow_redirects=False)
        print(f"{url}: {ip}: {response.status_code}", file=sys.stderr)
        return (True, response.status_code, ip, url)

    except Exception as e:
        print(f"{url}: {ip}: {type(e).__name__}", file=sys.stderr)
        return (False, type(e).__name__, ip, url)

async def main():
    urls = sys.stdin.readlines()
    if not urls:
        print("No url provided.")
        return

    async with httpx.AsyncClient() as client:
        tasks = [check_url(client, url) for url in urls]
        results = await asyncio.gather(*tasks)

    results = [r for r in results if r is not None]
    ok = [r for r in results if r[0]]
    fail = [r for r in results if not r[0]]

    print('====================failed====================', file=sys.stderr)
    for _, code, ip, url in fail:
        print(f"{url}: {ip}: {code}")

    print(f"\n{len(ok)}/{len(results)} OK", file=sys.stderr)

if __name__ == "__main__":
    try:
        asyncio.run(main())
    except KeyboardInterrupt:
        pass