Browse Source

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

pull/881/head
Hamish Coleman 3 years ago
committed by GitHub
parent
commit
999b3ffbf3
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 6
      scripts/n2n-ctl
  2. 10
      scripts/n2n-httpd

6
scripts/n2n-ctl

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

10
scripts/n2n-httpd

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

Loading…
Cancel
Save