"""Test if steady 5s interval avoids rate limit"""
import urllib.request, json, time

UA = 'Mozilla/5.0'
codes = ['00700', '01888', '00981']

for i, code in enumerate(codes):
    url = f"https://push2.eastmoney.com/api/qt/stock/get?secid=116.{code}&fields=f43,f170&fltt=2"
    start = time.time()
    try:
        req = urllib.request.Request(url, headers={"User-Agent": UA})
        with urllib.request.urlopen(req, timeout=5) as r:
            resp = json.loads(r.read().decode("utf-8"))
            elapsed = time.time() - start
            price = resp.get('data', {}).get('f43', '?')
            print(f"#{i+1} {code}: OK in {elapsed:.1f}s, price={price}")
    except Exception as e:
        elapsed = time.time() - start
        print(f"#{i+1} {code}: FAIL in {elapsed:.1f}s — {type(e).__name__}")
        break
    if i < len(codes) - 1:
        time.sleep(5)
