44
44
close ('127.0.0.1', 31415)
45
45
46
46
Client with JsonRPC2.0 and an abstract Unix Domain Socket::
47
-
47
+
48
48
>>> proxy = ServerProxy( JsonRpc20(), TransportUnixSocket(addr="\\ x00.rpcsocket") )
49
49
>>> proxy.hi( message="hello" ) #named parameters
50
50
u'hi there'
56
56
u'hello world'
57
57
58
58
Server with JsonRPC2.0 and abstract Unix Domain Socket with a logfile::
59
-
59
+
60
60
>>> server = Server( JsonRpc20(),
10000
TransportUnixSocket(addr="\\ x00.rpcsocket", logfunc=log_file("mylog.txt")) )
61
61
>>> def echo( s ):
62
62
... return s
174
174
PERMISSION_DENIED : "Permission denied." ,
175
175
INVALID_PARAM_VALUES : "Invalid parameter values."
176
176
}
177
-
177
+
178
178
#----------------------
179
179
# exceptions
180
180
@@ -189,7 +189,7 @@ class RPCTimeoutError(RPCTransportError):
189
189
190
190
class RPCFault (RPCError ):
191
191
"""RPC error/fault package received.
192
-
192
+
193
193
This exception can also be used as a class, to generate a
194
194
RPC-error/fault message.
195
195
@@ -304,7 +304,7 @@ def dumps_request( self, method, params=(), id=0 ):
304
304
- id: if id=None, this results in a Notification
305
305
:Returns: | {"method": "...", "params": ..., "id": ...}
306
306
| "method", "params" and "id" are always in this order.
307
- :Raises: TypeError if method/params is of wrong type or
307
+ :Raises: TypeError if method/params is of wrong type or
308
308
not JSON-serializable
309
309
"""
310
310
if not isinstance (method , (str , unicode )):
@@ -346,7 +346,7 @@ def dumps_error( self, error, id=None ):
346
346
347
347
Since JSON-RPC 1.0 does not define an error-object, this uses the
348
348
JSON-RPC 2.0 error-object.
349
-
349
+
350
350
:Parameters:
351
351
- error: a RPCFault instance
352
352
:Returns: | {"result": null, "error": {"code": error_code, "message": error_message, "data": error_data}, "id": ...}
@@ -480,7 +480,7 @@ def dumps_request( self, method, params=(), id=0 ):
480
480
:Returns: | {"jsonrpc": "2.0", "method": "...", "params": ..., "id": ...}
481
481
| "jsonrpc", "method", "params" and "id" are always in this order.
482
482
| "params" is omitted if empty
483
- :Raises: TypeError if method/params is of wrong type or
483
+ :Raises: TypeError if method/params is of wrong type or
484
484
not JSON-serializable
485
485
"""
486
486
if not isinstance (method , (str , unicode )):
@@ -527,7 +527,7 @@ def dumps_response( self, result, id=None ):
527
527
528
528
def dumps_error ( self , error , id = None ):
529
529
"""serialize a JSON-RPC-Response-error
530
-
530
+
531
531
:Parameters:
532
532
- error: a RPCFault instance
533
533
:Returns: | {"jsonrpc": "2.0", "error": {"code": error_code, "message": error_message, "data": error_data}, "id": ...}
@@ -675,7 +675,7 @@ def logfile( message ):
675
675
676
676
class Transport :
677
677
"""generic Transport-interface.
678
-
678
+
679
679
This class, and especially its methods and docstrings,
680
680
define the Transport-Interface.
681
681
"""
@@ -695,7 +695,7 @@ def sendrecv( self, string ):
695
695
return self .recv ()
696
696
def serve ( self , handler , n = None ):
697
697
"""serve (forever or for n communicaions).
698
-
698
+
699
699
- receive data
700
700
- call result = handler(data)
701
701
- send back result if not None
@@ -736,7 +736,7 @@ def recv(self):
736
736
import socket , select
737
737
class TransportSocket (Transport ):
738
738
"""Transport via socket.
739
-
739
+
740
740
:SeeAlso: python-module socket
741
741
:TODO:
742
742
- documentation
@@ -771,7 +771,7 @@ def close( self ):
771
771
self .s = None
772
772
def __repr__ (self ):
773
773
return "<TransportSocket, %s>" % repr (self .addr )
774
-
774
+
775
775
def send ( self , string ):
776
776
if self .s is None :
777
777
self .connect ()
@@ -798,7 +798,7 @@ def sendrecv( self, string ):
798
798
self .close ()
799
799
def serve (self , handler , n = None ):
800
800
"""open socket, wait for incoming connections and handle them.
801
-
801
+
802
802
:Parameters:
803
803
- n: serve n requests, None=forever
804
804
"""
@@ -828,7 +828,7 @@ def serve(self, handler, n=None):
828
828
829
829
830
830
if hasattr (socket , 'AF_UNIX' ):
831
-
831
+
832
832
class TransportUnixSocket (TransportSocket ):
833
833
"""Transport via Unix Domain Socket.
834
834
"""
@@ -846,7 +846,7 @@ def __init__(self, addr=None, limit=4096, timeout=5.0, logfunc=log_dummy):
846
846
class TransportTcpIp (TransportSocket ):
847
847
"""Transport via TCP/IP.
848
848
"""
849
- def __init__ (self , addr = None , limit = 4096 , timeout = 5 .0 , logfunc = log_dummy ):
849
+ def __init__ (self , addr = None , limit = 12000000 , timeout = 30 .0 , logfunc = log_dummy ):
850
850
"""
851
851
:Parameters:
852
852
- addr: ("host",port)
@@ -939,7 +939,7 @@ def __call__(self, *args, **kwargs):
939
939
class Server :
940
940
"""RPC-server.
941
941
942
- It works with different data/serializers and
942
+ It works with different data/serializers and
943
943
with different transports.
944
944
945
945
:Example:
@@ -980,7 +980,7 @@ def log(self, message):
980
980
981
981
def register_instance (self , myinst , name = None ):
982
982
"""Add all functions of a class-instance to the RPC-services.
983
-
983
+
984
984
All entries of the instance which do not begin with '_' are added.
985
985
986
986
:Parameters:
@@ -1000,7 +1000,7 @@ def register_instance(self, myinst, name=None):
1000
1000
self .register_function ( getattr (myinst , e ), name = "%s.%s" % (name , e ) )
1001
1001
def register_function (self , function , name = None ):
1002
1002
"""Add a function to the RPC-services.
1003
-
1003
+
1004
1004
:Parameters:
1005
1005
- function: function to add
1006
1006
- name: RPC-name for the function. If omitted/None, the original
@@ -1010,7 +1010,7 @@ def register_function(self, function, name=None):
1010
1010
self .funcs [function .__name__ ] = function
1011
1011
else :
1012
1012
self .funcs [name ] = function
1013
-
1013
+
1014
1014
def handle (self , rpcstr ):
1015
1015
"""Handle a RPC-Request.
1016
1016
@@ -1064,10 +1064,9 @@ def handle(self, rpcstr):
1064
1064
1065
1065
def serve (self , n = None ):
1066
1066
"""serve (forever or for n communicaions).
1067
-
1067
+
1068
1068
:See: Transport
1069
1069
"""
1070
1070
self .__transport .serve ( self .handle , n )
1071
1071
1072
1072
#=========================================
1073
-
0 commit comments