From a4c66b80aca5e3856905078984f654e77fa3a2e8 Mon Sep 17 00:00:00 2001 From: vladimirsvsv77 Date: Wed, 14 Mar 2018 13:24:39 +0300 Subject: [PATCH 1/3] update jsonrpc.py for py3 version --- jsonrpc.py | 57 ++++++++++++++++++++++++++---------------------------- 1 file changed, 27 insertions(+), 30 deletions(-) diff --git a/jsonrpc.py b/jsonrpc.py index 913cac0..a5b1640 100644 --- a/jsonrpc.py +++ b/jsonrpc.py @@ -1,5 +1,3 @@ -#!/usr/bin/env python -# -*- coding: ascii -*- """ JSON-RPC (remote procedure call). @@ -265,7 +263,7 @@ def dictkeyclean(d): :Raises: UnicodeEncodeError """ new_d = {} - for (k, v) in d.iteritems(): + for (k, v) in d.items(): new_d[str(k)] = v return new_d @@ -307,7 +305,7 @@ def dumps_request( self, method, params=(), id=0 ): :Raises: TypeError if method/params is of wrong type or not JSON-serializable """ - if not isinstance(method, (str, unicode)): + if not isinstance(method, str): raise TypeError('"method" must be a string (or unicode string).') if not isinstance(params, (tuple, list)): raise TypeError("params must be a tuple/list.") @@ -323,7 +321,7 @@ def dumps_notification( self, method, params=() ): | "method", "params" and "id" are always in this order. :Raises: see dumps_request """ - if not isinstance(method, (str, unicode)): + if not isinstance(method, str): raise TypeError('"method" must be a string (or unicode string).') if not isinstance(params, (tuple, list)): raise TypeError("params must be a tuple/list.") @@ -373,11 +371,11 @@ def loads_request( self, string ): """ try: data = self.loads(string) - except ValueError, err: + except Exception as err: raise RPCParseError("No valid JSON. (%s)" % str(err)) if not isinstance(data, dict): raise RPCInvalidRPC("No valid RPC-package.") if "method" not in data: raise RPCInvalidRPC("""Invalid Request, "method" is missing.""") - if not isinstance(data["method"], (str, unicode)): + if not isinstance(data["method"], str): raise RPCInvalidRPC("""Invalid Request, "method" must be a string.""") if "id" not in data: data["id"] = None #be liberal if "params" not in data: data["params"] = () #be liberal @@ -401,7 +399,7 @@ def loads_response( self, string ): """ try: data = self.loads(string) - except ValueError, err: + except Exception as err: raise RPCParseError("No valid JSON. (%s)" % str(err)) if not isinstance(data, dict): raise RPCInvalidRPC("No valid RPC-package.") if "id" not in data: raise RPCInvalidRPC("""Invalid Response, "id" missing.""") @@ -483,7 +481,7 @@ def dumps_request( self, method, params=(), id=0 ): :Raises: TypeError if method/params is of wrong type or not JSON-serializable """ - if not isinstance(method, (str, unicode)): + if not isinstance(method, str): raise TypeError('"method" must be a string (or unicode string).') if not isinstance(params, (tuple, list, dict)): raise TypeError("params must be a tuple/list/dict or None.") @@ -503,7 +501,7 @@ def dumps_notification( self, method, params=() ): | "jsonrpc", "method" and "params" are always in this order. :Raises: see dumps_request """ - if not isinstance(method, (str, unicode)): + if not isinstance(method, str): raise TypeError('"method" must be a string (or unicode string).') if not isinstance(params, (tuple, list, dict)): raise TypeError("params must be a tuple/list/dict or None.") @@ -554,15 +552,15 @@ def loads_request( self, string ): """ try: data = self.loads(string) - except ValueError, err: + except Exception as err: raise RPCParseError("No valid JSON. (%s)" % str(err)) if not isinstance(data, dict): raise RPCInvalidRPC("No valid RPC-package.") if "jsonrpc" not in data: raise RPCInvalidRPC("""Invalid Response, "jsonrpc" missing.""") - if not isinstance(data["jsonrpc"], (str, unicode)): + if not isinstance(data["jsonrpc"], str): raise RPCInvalidRPC("""Invalid Response, "jsonrpc" must be a string.""") if data["jsonrpc"] != "2.0": raise RPCInvalidRPC("""Invalid jsonrpc version.""") if "method" not in data: raise RPCInvalidRPC("""Invalid Request, "method" is missing.""") - if not isinstance(data["method"], (str, unicode)): + if not isinstance(data["method"], str): raise RPCInvalidRPC("""Invalid Request, "method" must be a string.""") if "params" not in data: data["params"] = () #convert params-keys from unicode to str @@ -589,12 +587,12 @@ def loads_response( self, string ): :Raises: | RPCFault+derivates for error-packages/faults, RPCParseError, RPCInvalidRPC """ try: - data = self.loads(string) - except ValueError, err: + data = self.loads(string.decode("utf-8")) + except Exception as err: raise RPCParseError("No valid JSON. (%s)" % str(err)) if not isinstance(data, dict): raise RPCInvalidRPC("No valid RPC-package.") if "jsonrpc" not in data: raise RPCInvalidRPC("""Invalid Response, "jsonrpc" missing.""") - if not isinstance(data["jsonrpc"], (str, unicode)): + if not isinstance(data["jsonrpc"], str): raise RPCInvalidRPC("""Invalid Response, "jsonrpc" must be a string.""") if data["jsonrpc"] != "2.0": raise RPCInvalidRPC("""Invalid jsonrpc version.""") if "id" not in data: raise RPCInvalidRPC("""Invalid Response, "id" missing.""") @@ -653,7 +651,7 @@ def log_dummy( message ): pass def log_stdout( message ): """print message to STDOUT""" - print message + print (message) def log_file( filename ): """return a logfunc which logs to a file (in utf-8)""" @@ -725,11 +723,11 @@ class TransportSTDINOUT(Transport): """ def send(self, string): """write data to STDOUT with '***SEND:' prefix """ - print "***SEND:" - print string + print ("***SEND:") + print (string) def recv(self): """read data from STDIN""" - print "***RECV (please enter, ^D ends.):" + print ("***RECV (please enter, ^D ends.):") return sys.stdin.read() @@ -776,7 +774,7 @@ def send( self, string ): if self.s is None: self.connect() self.log( "--> "+repr(string) ) - self.s.sendall( string ) + self.s.sendall( str.encode(string) ) def recv( self ): if self.s is None: self.connect() @@ -792,7 +790,7 @@ def recv( self ): def sendrecv( self, string ): """send data + receive data + close""" try: - self.send( string ) + self.send( string ) return self.recv() finally: self.close() @@ -902,7 +900,7 @@ def __req( self, methodname, args=None, kwargs=None, id=0 ): req_str = self.__data_serializer.dumps_request( methodname, kwargs, id ) try: resp_str = self.__transport.sendrecv( req_str ) - except Exception,err: + except Exception as err: raise RPCTransportError(err) resp = self.__data_serializer.loads_response( resp_str ) return resp[0] @@ -1028,9 +1026,9 @@ def handle(self, rpcstr): notification = True else: #request method, params, id = req - except RPCFault, err: + except RPCFault as err: return self.__data_serializer.dumps_error( err, id=None ) - except Exception, err: + except Exception as err: self.log( "%d (%s): %s" % (INTERNAL_ERROR, ERROR_MESSAGE[INTERNAL_ERROR], str(err)) ) return self.__data_serializer.dumps_error( RPCFault(INTERNAL_ERROR, ERROR_MESSAGE[INTERNAL_ERROR]), id=None ) @@ -1044,11 +1042,11 @@ def handle(self, rpcstr): result = self.funcs[method]( **params ) else: result = self.funcs[method]( *params ) - except RPCFault, err: + except RPCFault as err: if notification: return None return self.__data_serializer.dumps_error( err, id=None ) - except Exception, err: + except Exception as err: if notification: return None self.log( "%d (%s): %s" % (INTERNAL_ERROR, ERROR_MESSAGE[INTERNAL_ERROR], str(err)) ) @@ -1058,7 +1056,7 @@ def handle(self, rpcstr): return None try: return self.__data_serializer.dumps_response( result, id ) - except Exception, err: + except Exception as err: self.log( "%d (%s): %s" % (INTERNAL_ERROR, ERROR_MESSAGE[INTERNAL_ERROR], str(err)) ) return self.__data_serializer.dumps_error( RPCFault(INTERNAL_ERROR, ERROR_MESSAGE[INTERNAL_ERROR]), id ) @@ -1069,5 +1067,4 @@ def serve(self, n=None): """ self.__transport.serve( self.handle, n ) -#========================================= - +#========================================= \ No newline at end of file From ab47b250e7bfc2cc33b244b5e6ef5ddd5197fcf1 Mon Sep 17 00:00:00 2001 From: vladimirsvsv77 Date: Wed, 14 Mar 2018 13:31:13 +0300 Subject: [PATCH 2/3] fix Exception --- jsonrpc.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/jsonrpc.py b/jsonrpc.py index a5b1640..d5bcd40 100644 --- a/jsonrpc.py +++ b/jsonrpc.py @@ -371,7 +371,7 @@ def loads_request( self, string ): """ try: data = self.loads(string) - except Exception as err: + except ValueError as err: raise RPCParseError("No valid JSON. (%s)" % str(err)) if not isinstance(data, dict): raise RPCInvalidRPC("No valid RPC-package.") if "method" not in data: raise RPCInvalidRPC("""Invalid Request, "method" is missing.""") @@ -399,7 +399,7 @@ def loads_response( self, string ): """ try: data = self.loads(string) - except Exception as err: + except ValueError as err: raise RPCParseError("No valid JSON. (%s)" % str(err)) if not isinstance(data, dict): raise RPCInvalidRPC("No valid RPC-package.") if "id" not in data: raise RPCInvalidRPC("""Invalid Response, "id" missing.""") @@ -552,7 +552,7 @@ def loads_request( self, string ): """ try: data = self.loads(string) - except Exception as err: + except ValueError as err: raise RPCParseError("No valid JSON. (%s)" % str(err)) if not isinstance(data, dict): raise RPCInvalidRPC("No valid RPC-package.") if "jsonrpc" not in data: raise RPCInvalidRPC("""Invalid Response, "jsonrpc" missing.""") @@ -588,7 +588,7 @@ def loads_response( self, string ): """ try: data = self.loads(string.decode("utf-8")) - except Exception as err: + except ValueError as err: raise RPCParseError("No valid JSON. (%s)" % str(err)) if not isinstance(data, dict): raise RPCInvalidRPC("No valid RPC-package.") if "jsonrpc" not in data: raise RPCInvalidRPC("""Invalid Response, "jsonrpc" missing.""") From 2858c73afa8a1285184e0822506809ab72740e48 Mon Sep 17 00:00:00 2001 From: vladimirsvsv77 Date: Wed, 14 Mar 2018 13:32:51 +0300 Subject: [PATCH 3/3] remove space --- jsonrpc.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/jsonrpc.py b/jsonrpc.py index d5bcd40..402f370 100644 --- a/jsonrpc.py +++ b/jsonrpc.py @@ -790,7 +790,7 @@ def recv( self ): def sendrecv( self, string ): """send data + receive data + close""" try: - self.send( string ) + self.send( string ) return self.recv() finally: self.close() @@ -1067,4 +1067,4 @@ def serve(self, n=None): """ self.__transport.serve( self.handle, n ) -#========================================= \ No newline at end of file +#=========================================