1 Metadata-Version: 1.1
\r
2 Name: jsonrpclib-pelix
\r
4 Summary: This project is an implementation of the JSON-RPC v2.0 specification (backwards-compatible) as a client library, for Python 2.6+ and Python 3.This version is a fork of jsonrpclib by Josh Marshall, usable with Pelix remote services.
\r
5 Home-page: http://github.com/tcalmant/jsonrpclib/
\r
6 Author: Thomas Calmant
\r
8 License: Apache License 2.0
\r
9 Description: JSONRPClib (patched for Pelix)
\r
10 ##############################
\r
12 .. image:: https://pypip.in/license/jsonrpclib-pelix/badge.svg
\r
13 :target: https://pypi.python.org/pypi/jsonrpclib-pelix/
\r
15 .. image:: https://travis-ci.org/tcalmant/jsonrpclib.svg?branch=master
\r
16 :target: https://travis-ci.org/tcalmant/jsonrpclib
\r
18 .. image:: https://coveralls.io/repos/tcalmant/jsonrpclib/badge.svg?branch=master
\r
19 :target: https://coveralls.io/r/tcalmant/jsonrpclib?branch=master
\r
22 This library is an implementation of the JSON-RPC specification.
\r
23 It supports both the original 1.0 specification, as well as the
\r
24 new (proposed) 2.0 specification, which includes batch submission, keyword
\r
27 It is licensed under the Apache License, Version 2.0
\r
28 (http://www.apache.org/licenses/LICENSE-2.0.html).
\r
34 This is a patched version of the original ``jsonrpclib`` project by
\r
35 Josh Marshall, available at https://github.com/joshmarshall/jsonrpclib.
\r
37 The suffix *-pelix* only indicates that this version works with Pelix Remote
\r
38 Services, but it is **not** a Pelix specific implementation.
\r
40 * This version adds support for Python 3, staying compatible with Python 2.
\r
41 * It is now possible to use the dispatch_method argument while extending
\r
42 the SimpleJSONRPCDispatcher, to use a custom dispatcher.
\r
43 This allows to use this package by Pelix Remote Services.
\r
44 * It can use thread pools to control the number of threads spawned to handle
\r
45 notification requests and clients connections.
\r
46 * The modifications added in other forks of this project have been added:
\r
48 * From https://github.com/drdaeman/jsonrpclib:
\r
50 * Improved JSON-RPC 1.0 support
\r
51 * Less strict error response handling
\r
53 * From https://github.com/tuomassalo/jsonrpclib:
\r
55 * In case of a non-pre-defined error, raise an AppError and give access to
\r
58 * From https://github.com/dejw/jsonrpclib:
\r
60 * Custom headers can be sent with request and associated tests
\r
62 * The support for Unix sockets has been removed, as it is not trivial to convert
\r
63 to Python 3 (and I don't use them)
\r
64 * This version cannot be installed with the original ``jsonrpclib``, as it uses
\r
65 the same package name.
\r
71 This library implements the JSON-RPC 2.0 proposed specification in pure Python.
\r
72 It is designed to be as compatible with the syntax of ``xmlrpclib`` as possible
\r
73 (it extends where possible), so that projects using ``xmlrpclib`` could easily
\r
74 be modified to use JSON and experiment with the differences.
\r
76 It is backwards-compatible with the 1.0 specification, and supports all of the
\r
77 new proposed features of 2.0, including:
\r
79 * Batch submission (via MultiCall)
\r
81 * Notifications (both in a batch and 'normal')
\r
82 * Class translation using the ``__jsonclass__`` key.
\r
84 I've added a "SimpleJSONRPCServer", which is intended to emulate the
\r
85 "SimpleXMLRPCServer" from the default Python distribution.
\r
91 It supports ``cjson`` and ``simplejson``, and looks for the parsers in that
\r
92 order (searching first for ``cjson``, then for the *built-in* ``json`` in 2.6+,
\r
93 and then the ``simplejson`` external library).
\r
94 One of these must be installed to use this library, although if you have a
\r
95 standard distribution of 2.6+, you should already have one.
\r
96 Keep in mind that ``cjson`` is supposed to be the quickest, I believe, so if
\r
97 you are going for full-on optimization you may want to pick it up.
\r
99 Since library uses ``contextlib`` module, you should have at least Python 2.5
\r
106 You can install this from PyPI with one of the following commands (sudo
\r
109 .. code-block:: console
\r
111 easy_install jsonrpclib-pelix
\r
112 pip install jsonrpclib-pelix
\r
114 Alternatively, you can download the source from the GitHub repository
\r
115 at http://github.com/tcalmant/jsonrpclib and manually install it
\r
116 with the following commands:
\r
118 .. code-block:: console
\r
120 git clone git://github.com/tcalmant/jsonrpclib.git
\r
122 python setup.py install
\r
125 SimpleJSONRPCServer
\r
126 *******************
\r
128 This is identical in usage (or should be) to the SimpleXMLRPCServer in the
\r
129 Python standard library. Some of the differences in features are that it
\r
130 obviously supports notification, batch calls, class translation (if left on),
\r
132 Note: The import line is slightly different from the regular SimpleXMLRPCServer,
\r
133 since the SimpleJSONRPCServer is distributed within the ``jsonrpclib`` library.
\r
135 .. code-block:: python
\r
137 from jsonrpclib.SimpleJSONRPCServer import SimpleJSONRPCServer
\r
139 server = SimpleJSONRPCServer(('localhost', 8080))
\r
140 server.register_function(pow)
\r
141 server.register_function(lambda x,y: x+y, 'add')
\r
142 server.register_function(lambda x: x, 'ping')
\r
143 server.serve_forever()
\r
145 To start protect the server with SSL, use the following snippet:
\r
147 .. code-block:: python
\r
149 from jsonrpclib.SimpleJSONRPCServer import SimpleJSONRPCServer
\r
151 # Setup the SSL socket
\r
152 server = SimpleJSONRPCServer(('localhost', 8080), bind_and_activate=False)
\r
153 server.socket = ssl.wrap_socket(server.socket, certfile='server.pem',
\r
155 server.server_bind()
\r
156 server.server_activate()
\r
158 # ... register functions
\r
160 server.serve_forever()
\r
163 Notification Thread Pool
\r
164 ========================
\r
166 By default, notification calls are handled in the request handling thread.
\r
167 It is possible to use a thread pool to handle them, by giving it to the server
\r
168 using the ``set_notification_pool()`` method:
\r
170 .. code-block:: python
\r
172 from jsonrpclib.SimpleJSONRPCServer import SimpleJSONRPCServer
\r
173 from jsonrpclib.threadpool import ThreadPool
\r
175 # Setup the thread pool: between 0 and 10 threads
\r
176 pool = ThreadPool(max_threads=10, min_threads=0)
\r
178 # Don't forget to start it
\r
182 server = SimpleJSONRPCServer(('localhost', 8080), config)
\r
183 server.set_notification_pool(pool)
\r
186 server.register_function(pow)
\r
187 server.register_function(lambda x,y: x+y, 'add')
\r
188 server.register_function(lambda x: x, 'ping')
\r
191 server.serve_forever()
\r
193 # Stop the thread pool (let threads finish their current task)
\r
195 server.set_notification_pool(None)
\r
201 It is also possible to use a thread pool to handle clients requests, using the
\r
202 ``PooledJSONRPCServer`` class.
\r
203 By default, this class uses pool of 0 to 30 threads. A custom pool can be given
\r
204 with the ``thread_pool`` parameter of the class constructor.
\r
206 The notification pool and the request pool are different: by default, a server
\r
207 with a request pool doesn't have a notification pool.
\r
209 .. code-block:: python
\r
211 from jsonrpclib.SimpleJSONRPCServer import PooledJSONRPCServer
\r
212 from jsonrpclib.threadpool import ThreadPool
\r
214 # Setup the notification and request pools
\r
215 nofif_pool = ThreadPool(max_threads=10, min_threads=0)
\r
216 request_pool = ThreadPool(max_threads=50, min_threads=10)
\r
218 # Don't forget to start them
\r
220 request_pool.start()
\r
223 server = PooledJSONRPCServer(('localhost', 8080), config,
\r
224 thread_pool=request_pool)
\r
225 server.set_notification_pool(nofif_pool)
\r
228 server.register_function(pow)
\r
229 server.register_function(lambda x,y: x+y, 'add')
\r
230 server.register_function(lambda x: x, 'ping')
\r
233 server.serve_forever()
\r
235 # Stop the thread pools (let threads finish their current task)
\r
236 request_pool.stop()
\r
238 server.set_notification_pool(None)
\r
243 This is (obviously) taken from a console session.
\r
245 .. code-block:: python
\r
247 >>> import jsonrpclib
\r
248 >>> server = jsonrpclib.ServerProxy('http://localhost:8080')
\r
249 >>> server.add(5,6)
\r
251 >>> server.add(x=5, y=10)
\r
253 >>> server._notify.add(5,6)
\r
254 # No result returned...
\r
255 >>> batch = jsonrpclib.MultiCall(server)
\r
256 >>> batch.add(5, 6)
\r
257 >>> batch.ping({'key':'value'})
\r
258 >>> batch._notify.add(4, 30)
\r
259 >>> results = batch()
\r
260 >>> for result in results:
\r
261 >>> ... print(result)
\r
264 # Note that there are only two responses -- this is according to spec.
\r
267 >>> server('close')()
\r
269 # Using client history
\r
270 >>> history = jsonrpclib.history.History()
\r
271 >>> server = jsonrpclib.ServerProxy('http://localhost:8080', history=history)
\r
272 >>> server.add(5,6)
\r
274 >>> print(history.request)
\r
275 {"id": "f682b956-c8e1-4506-9db4-29fe8bc9fcaa", "jsonrpc": "2.0",
\r
276 "method": "add", "params": [5, 6]}
\r
277 >>> print(history.response)
\r
278 {"id": "f682b956-c8e1-4506-9db4-29fe8bc9fcaa", "jsonrpc": "2.0",
\r
282 >>> server('close')()
\r
284 If you need 1.0 functionality, there are a bunch of places you can pass that in,
\r
285 although the best is just to give a specific configuration to
\r
286 ``jsonrpclib.ServerProxy``:
\r
288 .. code-block:: python
\r
290 >>> import jsonrpclib
\r
291 >>> jsonrpclib.config.DEFAULT.version
\r
293 >>> config = jsonrpclib.config.Config(version=1.0)
\r
294 >>> history = jsonrpclib.history.History()
\r
295 >>> server = jsonrpclib.ServerProxy('http://localhost:8080', config=config,
\r
297 >>> server.add(7, 10)
\r
299 >>> print(history.request)
\r
300 {"id": "827b2923-5b37-49a5-8b36-e73920a16d32",
\r
301 "method": "add", "params": [7, 10]}
\r
302 >>> print(history.response)
\r
303 {"id": "827b2923-5b37-49a5-8b36-e73920a16d32", "error": null, "result": 17}
\r
304 >>> server('close')()
\r
306 The equivalent ``loads`` and ``dumps`` functions also exist, although with minor
\r
307 modifications. The ``dumps`` arguments are almost identical, but it adds three
\r
308 arguments: ``rpcid`` for the 'id' key, ``version`` to specify the JSON-RPC
\r
309 compatibility, and ``notify`` if it's a request that you want to be a
\r
312 Additionally, the ``loads`` method does not return the params and method like
\r
313 ``xmlrpclib``, but instead a.) parses for errors, raising ProtocolErrors, and
\r
314 b.) returns the entire structure of the request / response for manual parsing.
\r
320 If your remote service requires custom headers in request, you can pass them
\r
321 as as a ``headers`` keyword argument, when creating the ``ServerProxy``:
\r
323 .. code-block:: python
\r
325 >>> import jsonrpclib
\r
326 >>> server = jsonrpclib.ServerProxy("http://localhost:8080",
\r
327 headers={'X-Test' : 'Test'})
\r
329 You can also put additional request headers only for certain method invocation:
\r
331 .. code-block:: python
\r
333 >>> import jsonrpclib
\r
334 >>> server = jsonrpclib.Server("http://localhost:8080")
\r
335 >>> with server._additional_headers({'X-Test' : 'Test'}) as test_server:
\r
336 ... test_server.ping(42)
\r
338 >>> # X-Test header will be no longer sent in requests
\r
340 Of course ``_additional_headers`` contexts can be nested as well.
\r
346 I've recently added "automatic" class translation support, although it is
\r
347 turned off by default. This can be devastatingly slow if improperly used, so
\r
348 the following is just a short list of things to keep in mind when using it.
\r
350 * Keep It (the object) Simple Stupid. (for exceptions, keep reading.)
\r
351 * Do not require init params (for exceptions, keep reading)
\r
352 * Getter properties without setters could be dangerous (read: not tested)
\r
354 If any of the above are issues, use the _serialize method. (see usage below)
\r
355 The server and client must BOTH have use_jsonclass configuration item on and
\r
356 they must both have access to the same libraries used by the objects for
\r
359 If you have excessively nested arguments, it would be better to turn off the
\r
360 translation and manually invoke it on specific objects using
\r
361 ``jsonrpclib.jsonclass.dump`` / ``jsonrpclib.jsonclass.load`` (since the default
\r
362 behavior recursively goes through attributes and lists / dicts / tuples).
\r
364 Sample file: *test_obj.py*
\r
366 .. code-block:: python
\r
368 # This object is /very/ simple, and the system will look through the
\r
369 # attributes and serialize what it can.
\r
370 class TestObj(object):
\r
373 # This object requires __init__ params, so it uses the _serialize method
\r
374 # and returns a tuple of init params and attribute values (the init params
\r
375 # can be a dict or a list, but the attribute values must be a dict.)
\r
376 class TestSerial(object):
\r
378 def __init__(self, *args):
\r
380 def _serialize(self):
\r
381 return (self.args, {'foo':self.foo,})
\r
385 .. code-block:: python
\r
387 >>> import jsonrpclib
\r
388 >>> import test_obj
\r
390 # History is used only to print the serialized form of beans
\r
391 >>> history = jsonrpclib.history.History()
\r
392 >>> testobj1 = test_obj.TestObj()
\r
393 >>> testobj2 = test_obj.TestSerial()
\r
394 >>> server = jsonrpclib.Server('http://localhost:8080', history=history)
\r
396 # The 'ping' just returns whatever is sent
\r
397 >>> ping1 = server.ping(testobj1)
\r
398 >>> ping2 = server.ping(testobj2)
\r
400 >>> print(history.request)
\r
401 {"id": "7805f1f9-9abd-49c6-81dc-dbd47229fe13", "jsonrpc": "2.0",
\r
402 "method": "ping", "params": [{"__jsonclass__":
\r
403 ["test_obj.TestSerial", []], "foo": "bar"}
\r
405 >>> print(history.response)
\r
406 {"id": "7805f1f9-9abd-49c6-81dc-dbd47229fe13", "jsonrpc": "2.0",
\r
407 "result": {"__jsonclass__": ["test_obj.TestSerial", []], "foo": "bar"}}
\r
409 This behavior is turned by default. To deactivate it, just set the
\r
410 ``use_jsonclass`` member of a server ``Config`` to False.
\r
411 If you want to use a per-class serialization method, set its name in the
\r
412 ``serialize_method`` member of a server ``Config``.
\r
413 Finally, if you are using classes that you have defined in the implementation
\r
414 (as in, not a separate library), you'll need to add those (on BOTH the server
\r
415 and the client) using the ``config.classes.add()`` method.
\r
417 Feedback on this "feature" is very, VERY much appreciated.
\r
422 In my opinion, there are several reasons to choose JSON over XML for RPC:
\r
424 * Much simpler to read (I suppose this is opinion, but I know I'm right. :)
\r
425 * Size / Bandwidth - Main reason, a JSON object representation is just much smaller.
\r
426 * Parsing - JSON should be much quicker to parse than XML.
\r
427 * Easy class passing with ``jsonclass`` (when enabled)
\r
429 In the interest of being fair, there are also a few reasons to choose XML
\r
432 * Your server doesn't do JSON (rather obvious)
\r
433 * Wider XML-RPC support across APIs (can we change this? :))
\r
434 * Libraries are more established, i.e. more stable (Let's change this too.)
\r
439 Tests are an almost-verbatim drop from the JSON-RPC specification 2.0 page.
\r
440 They can be run using *unittest* or *nosetest*:
\r
442 .. code-block:: console
\r
444 python -m unittest discover tests
\r
445 python3 -m unittest discover tests
\r
449 Classifier: Development Status :: 5 - Production/Stable
\r
450 Classifier: Intended Audience :: Developers
\r
451 Classifier: License :: OSI Approved :: Apache Software License
\r
452 Classifier: Operating System :: OS Independent
\r
453 Classifier: Programming Language :: Python :: 2.6
\r
454 Classifier: Programming Language :: Python :: 2.7
\r
455 Classifier: Programming Language :: Python :: 3
\r
456 Classifier: Programming Language :: Python :: 3.0
\r
457 Classifier: Programming Language :: Python :: 3.1
\r
458 Classifier: Programming Language :: Python :: 3.2
\r
459 Classifier: Programming Language :: Python :: 3.3
\r
460 Classifier: Programming Language :: Python :: 3.4
\r