fixed python JSON clients so they don't hang if the daemon does not respond (or is not even running) (#876)

This commit is contained in:
Hamish Coleman 2021-10-25 13:51:47 +01:00 committed by GitHub
parent cea8bf8644
commit 999b3ffbf3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 1 deletions

View File

@ -19,6 +19,7 @@ class JsonUDP():
self.key = None
self.debug = False
self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.sock.settimeout(1)
def _next_tag(self):
tagstr = str(self.tag)
@ -239,7 +240,12 @@ def main():
rpc.debug = args.debug
rpc.key = args.key
try:
result = func(rpc, args)
except socket.timeout as e:
print(e)
exit(1)
print(result)

View File

@ -34,6 +34,7 @@ class JsonUDP():
self.key = None
self.debug = False
self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.sock.settimeout(1)
def _next_tag(self):
tagstr = str(self.tag)
@ -187,6 +188,9 @@ function rows2table(id, columns, data) {
function do_get(url, id, handler, handler_param) {
fetch(url)
.then(function (response) {
if (!response.ok) {
throw new Error('Fetch got ' + response.status)
}
return response.json();
})
.then(function (data) {
@ -205,6 +209,9 @@ function do_get(url, id, handler, handler_param) {
function do_post(url, body, id, handler, handler_param) {
fetch(url, {method:'POST', body: body})
.then(function (response) {
if (!response.ok) {
throw new Error('Fetch got ' + response.status)
}
return response.json();
})
.then(function (data) {
@ -438,6 +445,9 @@ class SimpleHandler(http.server.BaseHTTPRequestHandler):
self._simplereply(HTTPStatus.BAD_REQUEST, 'Bad Command')
return
except socket.timeout as e:
self._simplereply(HTTPStatus.REQUEST_TIMEOUT, str(e))
return
self._replyjson(data)
return