Examples > device-code-catalog
COMPANION MOCK_DEVICE_AUTH.PY (4/5)
return self.send(200, "
Approved. Go back to your reader.
", "text/html")
return self.send(404, "Unknown code
try again", "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, """
Approve a reader
""", "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)
Next: 12 Companion mock_device_auth.py (5/5)