mirror of
https://github.com/ntop/n2n.git
synced 2024-09-19 16:41:11 +02:00
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:
parent
cea8bf8644
commit
999b3ffbf3
|
@ -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
|
||||||
|
|
||||||
result = func(rpc, args)
|
try:
|
||||||
|
result = func(rpc, args)
|
||||||
|
except socket.timeout as e:
|
||||||
|
print(e)
|
||||||
|
exit(1)
|
||||||
|
|
||||||
print(result)
|
print(result)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -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…
Reference in New Issue
Block a user