diff options
-rwxr-xr-x | src/fead.py | 16 |
1 files changed, 10 insertions, 6 deletions
diff --git a/src/fead.py b/src/fead.py index 370b623..5987020 100755 --- a/src/fead.py +++ b/src/fead.py @@ -1,6 +1,6 @@ #!/usr/bin/env python3 # Advert generator from web feeds -# Copyright (C) 2022 Nguyễn Gia Phong +# Copyright (C) 2022, 2024 Nguyễn Gia Phong # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU Affero General Public License as published @@ -18,7 +18,7 @@ __version__ = '0.1.3' from argparse import ArgumentParser, FileType, HelpFormatter -from asyncio import gather, open_connection, run +from asyncio import CancelledError, gather, open_connection, run from collections import namedtuple from datetime import datetime from email.utils import parsedate_to_datetime @@ -150,10 +150,14 @@ async def fetch(raw_url): reader, writer = await open_connection(url.hostname, 80) else: raise ValueError(f'unsupported URL scheme: {url.scheme}') - writer.write(REQUEST.format(f"{url.path or '/'}?{url.query}", - url.hostname).encode()) - response = HTTPResponse(BytesSocket(await reader.read())) - writer.close() + try: + writer.write(REQUEST.format(f"{url.path or '/'}?{url.query}", + url.hostname).encode()) + response = HTTPResponse(BytesSocket(await reader.read())) + except CancelledError: + return None # silence propagation + finally: + writer.close() response.begin() with response: |