#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""取证：线上下发的那份 index.html 是不是我改过的新版。"""
import hashlib
import io
import ssl
import urllib.parse
import urllib.request

ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE

local = io.open('/Users/liuxinyuan/Desktop/Hermes输出-工作类/工具/index.html', 'rb').read()
print('本地 工具/index.html  %d 字节  md5=%s  visitStats=%d  测试报告收集(活引用)=%d'
      % (len(local), hashlib.md5(local).hexdigest()[:10],
         local.count(b'visitStats'), local.count(b"{ id: 'testReport'")))
print()
cands = [
    'https://api.biorisales.site:8443/' + urllib.parse.quote('工具/index.html'),
    'https://api.biorisales.site/' + urllib.parse.quote('工具/index.html'),
    'http://127.0.0.1:8765/' + urllib.parse.quote('工具/index.html'),
]
for u in cands:
    try:
        r = urllib.request.urlopen(urllib.request.Request(u, headers={'User-Agent': 'probe'}),
                                   timeout=15, context=ctx)
        b = r.read()
        same = '★与本地一致' if hashlib.md5(b).hexdigest() == hashlib.md5(local).hexdigest() else '≠与本地不同'
        print('HTTP %s %7d 字节 md5=%s visitStats=%d %s  ← %.60s'
              % (r.status, len(b), hashlib.md5(b).hexdigest()[:10], b.count(b'visitStats'), same, u))
    except Exception as e:
        print('  失败 %-58s %s' % (u[:58], str(e)[:70]))
