# -*- coding: utf-8 -*-
import socket, io, sys, urllib.request, ssl, time
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding="utf-8")
ctx = ssl.create_default_context(); ctx.check_hostname=False; ctx.verify_mode=ssl.CERT_NONE
hosts = [
    ("stanford bulletin", "https://bulletin.stanford.edu/"),
    ("columbia bulletin", "https://bulletin.columbia.edu/"),
    ("upenn catalog", "https://catalog.upenn.edu/"),
    ("cmu catalog", "https://www.cmu.edu/education/"),
    ("nyu gsas", "https://as.nyu.edu/"),
    ("gatech grad", "https://admission.gatech.edu/graduate"),
    ("duke grad", "https://gradschool.duke.edu/"),
    ("jhu eng", "https://engineering.jhu.edu/"),
    ("umich rackham", "https://rackham.umich.edu/"),
    ("cornell grad", "https://www.gradschool.cornell.edu/"),
    ("uchicago grad", "https://gradschool.uchicago.edu/"),
    ("berkeley grad", "https://grad.berkeley.edu/"),
    ("ucla grad", "https://grad.ucla.edu/"),
    ("uw grad", "https://www.uw.edu/grad/"),
    ("ut toronto sgs", "https://www.sgs.utoronto.ca/"),
    ("ubc grad", "https://www.grad.ubc.ca/"),
]
for name, url in hosts:
    host = urllib.request.urlparse(url).netloc
    try:
        ip = socket.gethostbyname(host)
    except Exception as e:
        print(f"{name:22s} DNS FAIL {host} ({e.__class__.__name__})")
        continue
    try:
        req = urllib.request.Request(url, headers={"User-Agent":"Mozilla/5.0"})
        t0=time.time()
        r = urllib.request.urlopen(req, timeout=6, context=ctx)
        print(f"{name:22s} HTTP {r.status}  {host}  {len(r.read(200000))}B  {time.time()-t0:.1f}s")
    except Exception as e:
        print(f"{name:22s} HTTP FAIL {host} ({e.__class__.__name__}: {str(e)[:60]})")
