Skip to content

Instantly share code, notes, and snippets.

@lwzm
Last active December 25, 2018 03:11
Show Gist options
  • Save lwzm/45c923a6e2a08f506e04f7c2f13c3602 to your computer and use it in GitHub Desktop.
Save lwzm/45c923a6e2a08f506e04f7c2f13c3602 to your computer and use it in GitHub Desktop.
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment