From 72111bce703baabefbb1fb7e2438bfe84898285a Mon Sep 17 00:00:00 2001 From: dece Date: Fri, 5 Mar 2021 19:27:42 +0100 Subject: [PATCH] protocol: make connection timeout after some time --- bebop/browser.py | 6 +++++- bebop/protocol.py | 7 +++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/bebop/browser.py b/bebop/browser.py index 1e3494d..be81fd0 100644 --- a/bebop/browser.py +++ b/bebop/browser.py @@ -222,7 +222,11 @@ class Browser: else: pass # TODO - response = Response.parse(req.proceed()) + data = req.proceed() + if not data: + self.set_status_error(f"Server did not respond in time ({url}).") + return + response = Response.parse(data) if not response: self.set_status_error(f"Server response parsing failed ({url}).") return diff --git a/bebop/protocol.py b/bebop/protocol.py index 33356ec..fe42f01 100644 --- a/bebop/protocol.py +++ b/bebop/protocol.py @@ -81,7 +81,7 @@ class Request: self.payload += LINE_TERM try: - sock = socket.create_connection((hostname, port)) + sock = socket.create_connection((hostname, port), timeout=10) except OSError as exc: self.state = Request.STATE_CONNECTION_FAILED self.error = exc.strerror @@ -124,7 +124,10 @@ class Request: self.ssock.sendall(self.payload) response = b"" while True: - buf = self.ssock.recv(4096) + try: + buf = self.ssock.recv(4096) + except socket.timeout: + buf = None if not buf: return response response += buf