From d3c5e8dfcbf6d153e5d9c12ae308101f25577a96 Mon Sep 17 00:00:00 2001 From: Chris Needham Date: Wed, 13 Mar 2024 10:10:25 +0000 Subject: [PATCH] Added handling of HTTP status when requesting images --- lib/async_http_client.py | 11 ++++++++++- lib/main_frame.py | 7 +++---- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/lib/async_http_client.py b/lib/async_http_client.py index ab8e2d6..2ad5c70 100644 --- a/lib/async_http_client.py +++ b/lib/async_http_client.py @@ -15,6 +15,7 @@ import asyncore import logging import queue +import re import socket import threading import urllib @@ -95,7 +96,15 @@ def handle_expt(self): def handle_close(self): self.close() - self._client.http_received_data(self._data) + match = re.match(b'^HTTP/\d+\.\d+ (\d+)', self._header) + if match is not None: + status = int(match.group(1)) + self.log("Response: HTTP status %d" % (status)) + + if status >= 200 and status <= 299: + self._client.http_received_data(self._data) + else: + self._client.http_closed() self._header = None self._data = b"" diff --git a/lib/main_frame.py b/lib/main_frame.py index 3d3b492..48be3c5 100644 --- a/lib/main_frame.py +++ b/lib/main_frame.py @@ -699,11 +699,10 @@ def create_bitmap(self, image_data): bitmap = None - try: - image = wx.Image(stream) + image = wx.Image(stream) + if image.IsOk(): bitmap = wx.Bitmap(image) - except wx.wxAssertionError as ex: - # Invalid image data. + else: logging.warning("Couldn't create image") return bitmap