Last active
December 25, 2018 03:11
-
-
Save lwzm/45c923a6e2a08f506e04f7c2f13c3602 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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