From f3467cf7764de5ca2ceaadb7e797c2b7f9e51d08 Mon Sep 17 00:00:00 2001 From: Eran Keydar Date: Fri, 3 Oct 2014 00:09:05 +0300 Subject: [PATCH 1/8] code and reason for close callback --- websocket/_app.py | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/websocket/_app.py b/websocket/_app.py index d7e6d60..cb7fd2c 100644 --- a/websocket/_app.py +++ b/websocket/_app.py @@ -16,12 +16,15 @@ Copyright (C) 2010 Hiroki Ohtani(liris) You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - +> """ """ WebSocketApp provides higher level APIs. """ + +print 'INTERNAL VERSION' + import threading import time import traceback @@ -177,9 +180,26 @@ class WebSocketApp(object): thread.join() self.keep_running = False self.sock.close() - self._callback(self.on_close) + try: + data = frame.data + except NameError: + data = None + self._callback(self.on_close,*self._get_close_args(data)) self.sock = None + def _get_close_args(self,data): + """ this functions extracts the code, reason from the close body + if they exists, and if the self.on_close except three arguments """ + import inspect + if len(data) >=2: + code = 256*six.byte2int(data[0]) + six.byte2int(data[1]) + reason = data[2:].decode('utf-8') + if self.on_close and len(inspect.getargspec(self.on_close).args) == 3: + return [code,reason] + else: + return [] + return [] + def _callback(self, callback, *args): if callback: try: From aa4e2cf0a5d91b8a25501f22c5851903707ef936 Mon Sep 17 00:00:00 2001 From: Eran Keydar Date: Fri, 3 Oct 2014 00:10:26 +0300 Subject: [PATCH 2/8] code and reason for close callback --- websocket/_app.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/websocket/_app.py b/websocket/_app.py index cb7fd2c..2f27ffd 100644 --- a/websocket/_app.py +++ b/websocket/_app.py @@ -22,9 +22,6 @@ Copyright (C) 2010 Hiroki Ohtani(liris) """ WebSocketApp provides higher level APIs. """ - -print 'INTERNAL VERSION' - import threading import time import traceback From bc0c43eece25a60f5b93638c5159e81fde727312 Mon Sep 17 00:00:00 2001 From: Eran Keydar Date: Fri, 3 Oct 2014 00:12:16 +0300 Subject: [PATCH 3/8] code and reason for close callback --- websocket/_app.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/websocket/_app.py b/websocket/_app.py index 2f27ffd..066ab3b 100644 --- a/websocket/_app.py +++ b/websocket/_app.py @@ -16,7 +16,7 @@ Copyright (C) 2010 Hiroki Ohtani(liris) You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -> + """ """ From 03e52b8974443a1aeb01c799d6b2b1fc311f51f1 Mon Sep 17 00:00:00 2001 From: Eran Keydar Date: Fri, 3 Oct 2014 11:03:28 +0300 Subject: [PATCH 4/8] fix for null data --- websocket/_app.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/websocket/_app.py b/websocket/_app.py index 066ab3b..23e6c86 100644 --- a/websocket/_app.py +++ b/websocket/_app.py @@ -188,7 +188,7 @@ class WebSocketApp(object): """ this functions extracts the code, reason from the close body if they exists, and if the self.on_close except three arguments """ import inspect - if len(data) >=2: + if data and len(data) >=2: code = 256*six.byte2int(data[0]) + six.byte2int(data[1]) reason = data[2:].decode('utf-8') if self.on_close and len(inspect.getargspec(self.on_close).args) == 3: From 9854ea838933cf85a2d7ad110152b446cd000361 Mon Sep 17 00:00:00 2001 From: Eran Keydar Date: Fri, 3 Oct 2014 11:15:47 +0300 Subject: [PATCH 5/8] fix for expected arg 3 --- websocket/_app.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/websocket/_app.py b/websocket/_app.py index 23e6c86..fee5e81 100644 --- a/websocket/_app.py +++ b/websocket/_app.py @@ -188,14 +188,13 @@ class WebSocketApp(object): """ this functions extracts the code, reason from the close body if they exists, and if the self.on_close except three arguments """ import inspect + if not self.on_close and len(inspect.getargspec(self.on_close).args) == 3: + return [] if data and len(data) >=2: code = 256*six.byte2int(data[0]) + six.byte2int(data[1]) reason = data[2:].decode('utf-8') - if self.on_close and len(inspect.getargspec(self.on_close).args) == 3: return [code,reason] - else: - return [] - return [] + return [None,None] def _callback(self, callback, *args): if callback: From 61192853526503e3810259faf198bbb49a099936 Mon Sep 17 00:00:00 2001 From: Eran Keydar Date: Fri, 3 Oct 2014 11:16:36 +0300 Subject: [PATCH 6/8] fix for expected arg 3 --- websocket/_app.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/websocket/_app.py b/websocket/_app.py index fee5e81..68a1425 100644 --- a/websocket/_app.py +++ b/websocket/_app.py @@ -193,7 +193,7 @@ class WebSocketApp(object): if data and len(data) >=2: code = 256*six.byte2int(data[0]) + six.byte2int(data[1]) reason = data[2:].decode('utf-8') - return [code,reason] + return [code,reason] return [None,None] def _callback(self, callback, *args): From 6858ba08c7c98ec7bbac19beb0f824f5f2aebfe9 Mon Sep 17 00:00:00 2001 From: Eran Keydar Date: Sat, 4 Oct 2014 20:29:07 +0300 Subject: [PATCH 7/8] ws --- websocket/_app.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/websocket/_app.py b/websocket/_app.py index 68a1425..4a11ac6 100644 --- a/websocket/_app.py +++ b/websocket/_app.py @@ -131,6 +131,7 @@ class WebSocketApp(object): if self.sock: raise WebSocketException("socket is already opened") thread = None + break_on_close_op = False try: self.sock = WebSocket(self.get_mask_key, sockopt=sockopt, sslopt=sslopt, @@ -157,6 +158,7 @@ class WebSocketApp(object): if r: op_code, frame = self.sock.recv_data_frame(True) if op_code == ABNF.OPCODE_CLOSE: + break_on_close_op = True break elif op_code == ABNF.OPCODE_PING: self._callback(self.on_ping, frame.data) @@ -176,12 +178,8 @@ class WebSocketApp(object): event.set() thread.join() self.keep_running = False - self.sock.close() - try: - data = frame.data - except NameError: - data = None - self._callback(self.on_close,*self._get_close_args(data)) + self.sock.close() + self._callback(self.on_close,*self._get_close_args(frame.data if break_on_close_op else None)) self.sock = None def _get_close_args(self,data): From c8c30d683fd26b7cd4548fa0ab21d7b63eccd3c0 Mon Sep 17 00:00:00 2001 From: Eran Keydar Date: Sun, 5 Oct 2014 08:31:56 +0300 Subject: [PATCH 8/8] fix --- websocket/_app.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/websocket/_app.py b/websocket/_app.py index 4a11ac6..274fad6 100644 --- a/websocket/_app.py +++ b/websocket/_app.py @@ -178,7 +178,7 @@ class WebSocketApp(object): event.set() thread.join() self.keep_running = False - self.sock.close() + self.sock.close() self._callback(self.on_close,*self._get_close_args(frame.data if break_on_close_op else None)) self.sock = None @@ -186,7 +186,8 @@ class WebSocketApp(object): """ this functions extracts the code, reason from the close body if they exists, and if the self.on_close except three arguments """ import inspect - if not self.on_close and len(inspect.getargspec(self.on_close).args) == 3: + # if the on_close callback is "old", just return empty list + if not self.on_close or len(inspect.getargspec(self.on_close).args) != 3: return [] if data and len(data) >=2: code = 256*six.byte2int(data[0]) + six.byte2int(data[1])