From 4a10bfb093cef0f733ca49198b07495b64256666 Mon Sep 17 00:00:00 2001 From: Jordan Date: Sat, 2 Apr 2022 23:48:51 -0700 Subject: asn: catch connection timeouts (bots) and other exceptions --- asn.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) (limited to 'asn.py') diff --git a/asn.py b/asn.py index 8174a3c..6bc2c98 100755 --- a/asn.py +++ b/asn.py @@ -32,22 +32,22 @@ class Listener: args=(conn,addr,), daemon=True).start() def _handler(self, conn, addr): - recv_data = conn.recv(1024) - if not recv_data: - conn.close() - + resp = '' try: + recv_data = conn.recv(1024) recv_data = str(recv_data, 'utf-8').strip() + except ConnectionResetError: + log.info(f'{addr[0]} connection reset') except UnicodeDecodeError: - resp = 'could not decode query to utf-8' + log.info(f'{addr[0]} could not decode to utf-8') + except Exception as err: + log.info(f'{addr[0]} {err}') else: log.info(f'{addr[0]} {recv_data}') - resp = self._get_announcements(recv_data) - if resp: - resp = self._pretty(resp) - else: - resp = '' + announcements = self._get_announcements(recv_data) + if announcements: + resp = self._pretty(announcements) finally: conn.sendall(bytes(resp, 'utf-8')) conn.shutdown(socket.SHUT_RDWR) -- cgit v1.2.3-54-g00ecf