commit 9f7edfa033174934d993217ee0c3fde8502fc63d from: Aleksey Ryndin date: Fri Sep 13 10:07:26 2024 UTC Add: Proxy improvements: handle HTTPError and stream answer by chunks commit - b7194e0323fc165e03bd36a58ed584bd90811cfd commit + 9f7edfa033174934d993217ee0c3fde8502fc63d blob - b0b625fa8b5632a71f3467cb1cf284af80831cde blob + dd45d20eca583fc1e5830281346f47a43b4dcd63 --- lonk.py +++ lonk.py @@ -3,6 +3,7 @@ from sqlite3 import connect as sqlite3_connect from json import loads as json_loads from pathlib import Path from sys import stdout +from urllib.error import HTTPError from urllib.parse import urlencode, urlunsplit, urljoin, urlsplit, parse_qsl from urllib.request import urlopen from html.parser import HTMLParser @@ -247,10 +248,16 @@ def demo(lonk_url): def proxy(mime, url): - with urlopen(url, timeout=10) as f: - stdout.buffer.write(b"20 " + mime.encode() + b"\r\n") - stdout.buffer.write(f.read()) - + try: + with urlopen(url, timeout=10) as f: + stdout.buffer.write(b"20 " + mime.encode() + b"\r\n") + while True: + content = f.read(512 * 1024) + if not content: + break + stdout.buffer.write(content) + except HTTPError as error: + print(f"43 Remote server return {error.code}: {error.reason}\r") def vgi(cert_hash): url = urlsplit(input().strip())