lwzm
12/24/2018 - 4:42 AM

urlopen

from urllib.request import urlopen
url = "http://mg.bxs.tyio.net/static/logo.png"
url = "http://ng.tyio.net"
url = "http://google.com"
with urlopen(url, timeout=1) as f:
    print(f.read())  # bytes
from hashlib import md5
from urllib.request import urlopen, Request, HTTPError

url = "http://mg.bxs.tyio.net/static/logo.png"
url = "http://ng.tyio.net/"
url = "http://google.com"
fn = "." + md5(url.encode()).hexdigest()
headers = {}
try:
    with open(fn) as f:
        headers["if-modified-since"] = f.read()
except FileNotFoundError:
    pass

try:
    with urlopen(Request(url, headers=headers), timeout=1) as resp:
        last_modified = resp.headers["Last-Modified"]
        if last_modified:
            with open(fn, "w") as f:
                f.write(last_modified)
except HTTPError as e:
    print(e)