11 Companion mock_device_auth.py (4/5)
return self.send(200, "<h1>Approved. Go back to your reader.</h1>", "text/html")
return self.send(404, "<h1>Unknown code</h1><a href='/activate'>try again</a>", "text/html")
self.send(404, {"error": "not found"})
def do_GET(self):
url = urllib.parse.urlparse(self.path)
if url.path == "/activate":
return self.send(200, """<!doctype html><meta name=viewport content="width=device-width">
<h1>Approve a reader</h1><form method=post>
<input name=code placeholder="ABCD-1234" autofocus> <button>Approve</button></form>""", "text/html")
if url.path == "/api/books":
if not self.authed():
return
rows = [{"id": f.stem, "title": f.stem.replace("-", " ").title(),
"url": f"http://{self.headers['Host']}/files/{urllib.parse.quote(f.name)}"}
for f in sorted(BOOKS.glob("*.epub"))]
return self.send(200, {"books": rows})
if url.path.startswith("/files/"):
if not self.authed():
return
f = BOOKS / urllib.parse.unquote(url.path[len("/files/"):])
if f.parent != BOOKS or not f.is_file():
return self.send(404, {"error": "no file"})
return self.send(200, f.read_bytes(), "application/epub+zip")
self.send(404, {"error": "not found"})
def log_message(self, fmt, *args):
print(self.address_string(), fmt % args)