Tree @HEAD (Download .tar.gz)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 | # JSONRPClib (patched for Pelix and Python 3) [![Latest Version](https://img.shields.io/pypi/v/jsonrpclib-pelix.svg)](https://pypi.python.org/pypi/jsonrpclib-pelix/) [![License](https://img.shields.io/pypi/l/jsonrpclib-pelix.svg)](https://pypi.python.org/pypi/jsonrpclib-pelix/) [![Travis-CI status](https://travis-ci.org/tcalmant/jsonrpclib.svg?branch=master)](https://travis-ci.org/tcalmant/jsonrpclib) [![Coveralls status](https://coveralls.io/repos/tcalmant/jsonrpclib/badge.svg?branch=master)](https://coveralls.io/r/tcalmant/jsonrpclib?branch=master) This library is an implementation of the JSON-RPC specification. It supports both the original 1.0 specification, as well as the new (proposed) 2.0 specification, which includes batch submission, keyword arguments, etc. This library is licensed under the terms of the [Apache Software License 2.0](<http://www.apache.org/licenses/LICENSE-2.0.html>). ## About this version This is a patched version of the original `jsonrpclib` project by Josh Marshall, available at [joshmarshall/jsonrpclib](<https://github.com/joshmarshall/jsonrpclib>). The suffix *-pelix* only indicates that this version works with Pelix Remote Services, but it is **not** a Pelix specific implementation. * This version adds support for Python 3, staying compatible with Python 2.7. The support for Python 2.6 has been dropped, as it was becoming to hard to maintain. * It is now possible to use the `dispatch_method` argument while extending the `SimpleJSONRPCDispatcher`, to use a custom dispatcher. This allows to use this package by Pelix Remote Services. * It can use thread pools to control the number of threads spawned to handle notification requests and clients connections. * The modifications added in other forks of this project have been added: * From [drdaeman/jsonrpclib](<https://github.com/drdaeman/jsonrpclib>): * Improved JSON-RPC 1.0 support * Less strict error response handling * From [tuomassalo/jsonrpclib](<https://github.com/tuomassalo/jsonrpclib>): * In case of a non-predefined error, raise an AppError and give access to *error.data* * From [dejw/jsonrpclib](<https://github.com/dejw/jsonrpclib>): * Custom headers can be sent with request and associated tests * Since version 0.4, this package added back the support of Unix sockets. * This package cannot be installed with the original `jsonrpclib`, as it uses the same name. ## Summary This library implements the JSON-RPC 2.0 proposed specification in pure Python. It is designed to be as compatible with the syntax of `xmlrpclib` as possible (it extends where possible), so that projects using `xmlrpclib` could easily be modified to use JSON and experiment with the differences. It is backwards-compatible with the 1.0 specification, and supports all of the new proposed features of 2.0, including: - Batch submission (via the `MultiCall` class) - Keyword arguments - Notifications (both in a batch and 'normal') - Class translation using the `__jsonclass__` key. A `SimpleJSONRPCServer` class has been added. It is intended to emulate the `SimpleXMLRPCServer` from the default Python distribution. ## Requirements This library supports `cjson` and `simplejson`, and looks for the parsers in that order (searching first for `cjson`, then for the *built-in* `json` in 2.7+, and then the `simplejson` external library). One of these must be installed to use this library, although if you have a standard distribution of 2.7+, you should already have one. Keep in mind that `cjson` is supposed to be the quickest, I believe, so if you are going for full-on optimization you may want to pick it up. ## Installation You can install this from PyPI with one of the following commands (`sudo` might be required): ``` # Global installation pip install jsonrpclib-pelix # Local installation pip install --user jsonrpclib-pelix ``` Alternatively, you can download the source from the GitHub repository at [tcalmant/jsonrpclib](http://github.com/tcalmant/jsonrpclib) and manually install it with the following commands: ``` git clone git://github.com/tcalmant/jsonrpclib.git cd jsonrpclib python setup.py install ``` ## A note on logging `jsonrpclib-pelix` uses the `logging` module from the standard Python library to trace warnings and errors, but doesn't set it up. As a result, you have to configure the Python logging to print out traces. The easiest way to do it is to add those lines at the beginning of your code: ```python import logging logging.basiConfig() ``` More information can be found in the [`logging` documentation page](https://docs.python.org/3/library/logging.html). ## `SimpleJSONRPCServer` This is identical in usage (or should be) to the `SimpleXMLRPCServer` in the Python standard library. Some of the differences in features are that it obviously supports notification, batch calls, class translation (if left on), etc. **Note:** The import line is slightly different from the regular `SimpleXMLRPCServer`, since the `SimpleJSONRPCServer` is provided by th `jsonrpclib` library. ```python from jsonrpclib.SimpleJSONRPCServer import SimpleJSONRPCServer server = SimpleJSONRPCServer(('localhost', 8080)) server.register_function(pow) server.register_function(lambda x,y: x+y, 'add') server.register_function(lambda x: x, 'ping') server.serve_forever() ``` To start protect the server with SSL, use the following snippet: ```python from jsonrpclib.SimpleJSONRPCServer import SimpleJSONRPCServer import ssl # Setup the SSL socket server = SimpleJSONRPCServer(('localhost', 8080), bind_and_activate=False) server.socket = ssl.wrap_socket(server.socket, certfile='server.pem', server_side=True) server.server_bind() server.server_activate() # ... register functions # Start the server server.serve_forever() ``` ### Notification Thread Pool By default, notification calls are handled in the request handling thread. It is possible to use a thread pool to handle them, by giving it to the server using the `set_notification_pool()` method: ```python from jsonrpclib.SimpleJSONRPCServer import SimpleJSONRPCServer from jsonrpclib.threadpool import ThreadPool # Setup the thread pool: between 0 and 10 threads pool = ThreadPool(max_threads=10, min_threads=0) # Don't forget to start it pool.start() # Setup the server server = SimpleJSONRPCServer(('localhost', 8080)) server.set_notification_pool(pool) # Register methods server.register_function(pow) server.register_function(lambda x,y: x+y, 'add') server.register_function(lambda x: x, 'ping') try: server.serve_forever() finally: # Stop the thread pool (let threads finish their current task) pool.stop() server.set_notification_pool(None) ``` ### Threaded server It is also possible to use a thread pool to handle clients requests, using the `PooledJSONRPCServer` class. By default, this class uses pool of 0 to 30 threads. A custom pool can be given with the `thread_pool` parameter of the class constructor. The notification pool and the request pool are different: by default, a server with a request pool doesn't have a notification pool. ```python from jsonrpclib.SimpleJSONRPCServer import PooledJSONRPCServer from jsonrpclib.threadpool import ThreadPool # Setup the notification and request pools nofif_pool = ThreadPool(max_threads=10, min_threads=0) request_pool = ThreadPool(max_threads=50, min_threads=10) # Don't forget to start them nofif_pool.start() request_pool.start() # Setup the server server = PooledJSONRPCServer(('localhost', 8080), thread_pool=request_pool) server.set_notification_pool(nofif_pool) # Register methods server.register_function(pow) server.register_function(lambda x,y: x+y, 'add') server.register_function(lambda x: x, 'ping') try: server.serve_forever() finally: # Stop the thread pools (let threads finish their current task) request_pool.stop() nofif_pool.stop() server.set_notification_pool(None) ``` ### Unix socket To start a server listening on a Unix socket, you will have to use the following snippet: ```python from jsonrpclib.SimpleJSONRPCServer import SimpleJSONRPCServer import os import socket # Set the path to the socket file socket_name = "/tmp/my_socket.socket" # Ensure that the file doesn't exist yet (or an error will be raised) if os.path.exists(socket_name): os.remove(socket_name) try: # Start the server, indicating the socket family # The server will force some flags when in Unix socket mode # (no log request, no reuse address, ...) srv = SimpleJSONRPCServer(socket_name, address_family=socket.AF_UNIX) # ... register methods to the server # Run the server srv.serve_forever() except KeyboardInterrupt: # Shutdown the server gracefully srv.shutdown() srv.server_close() finally: # You should clean up after the server stopped os.remove(socket_name) ``` This feature is tested on Linux during Travis-CI builds. It also has been tested on Windows Subsystem for Linux (WSL) on Windows 10 1809. This feature is not available on "pure" Windows, as it doesn't provide the `AF_UNIX` address family. ## Client Usage This is (obviously) taken from a console session. ```python >>> import jsonrpclib >>> server = jsonrpclib.ServerProxy('http://localhost:8080') >>> server.add(5,6) 11 >>> server.add(x=5, y=10) 15 >>> server._notify.add(5,6) # No result returned... >>> batch = jsonrpclib.MultiCall(server) >>> batch.add(5, 6) >>> batch.ping({'key':'value'}) >>> batch._notify.add(4, 30) >>> results = batch() >>> for result in results: >>> ... print(result) 11 {'key': 'value'} # Note that there are only two responses -- this is according to spec. # Clean up >>> server('close')() # Using client history >>> history = jsonrpclib.history.History() >>> server = jsonrpclib.ServerProxy('http://localhost:8080', history=history) >>> server.add(5,6) 11 >>> print(history.request) {"id": "f682b956-c8e1-4506-9db4-29fe8bc9fcaa", "jsonrpc": "2.0", "method": "add", "params": [5, 6]} >>> print(history.response) {"id": "f682b956-c8e1-4506-9db4-29fe8bc9fcaa", "jsonrpc": "2.0", "result": 11} # Clean up >>> server('close')() ``` If you need 1.0 functionality, there are a bunch of places you can pass that in, although the best is just to give a specific configuration to `jsonrpclib.ServerProxy`: ```python >>> import jsonrpclib >>> jsonrpclib.config.DEFAULT.version 2.0 >>> config = jsonrpclib.config.Config(version=1.0) >>> history = jsonrpclib.history.History() >>> server = jsonrpclib.ServerProxy('http://localhost:8080', config=config, history=history) >>> server.add(7, 10) 17 >>> print(history.request) {"id": "827b2923-5b37-49a5-8b36-e73920a16d32", "method": "add", "params": [7, 10]} >>> print(history.response) {"id": "827b2923-5b37-49a5-8b36-e73920a16d32", "error": null, "result": 17} >>> server('close')() ``` The equivalent `loads` and `dumps` functions also exist, although with minor modifications. The `dumps` arguments are almost identical, but it adds three arguments: `rpcid` for the `id` key, `version` to specify the JSON-RPC compatibility, and `notify` if it's a request that you want to be a notification. Additionally, the `loads` method does not return the params and method like `xmlrpclib`, but instead a.) parses for errors, raising ProtocolErrors, and b.) returns the entire structure of the request / response for manual parsing. ### Unix sockets To connect a JSON-RPC server over a Unix socket, you have to use a specific protocol: `unix+http`. When connecting to a Unix socket in the current working directory, you can use the following syntax: `unix+http://my.socket` When you need to give an absolute path you must use the path part of the URL, the host part will be ignored. For example, you can use this URL to indicate a Unix socket in `/var/lib/daemon.socket`: `unix+http://./var/lib/daemon.socket` **Note:** Currently, only HTTP is supported over a Unix socket. If you want HTTPS support to be implemented, please create an [issue on GitHub](https://github.com/tcalmant/jsonrpclib/issues) ### Additional headers If your remote service requires custom headers in request, you can pass them using the `headers` keyword argument, when creating the `ServerProxy`: ```python >>> import jsonrpclib >>> server = jsonrpclib.ServerProxy("http://localhost:8080", headers={'X-Test' : 'Test'}) ``` You can also put additional request headers only for certain method invocation: ```python >>> import jsonrpclib >>> server = jsonrpclib.Server("http://localhost:8080") >>> with server._additional_headers({'X-Test' : 'Test'}) as test_server: ... test_server.ping(42) ... >>> # X-Test header will be no longer sent in requests ``` Of course `_additional_headers` contexts can be nested as well. ## Class Translation The library supports an *"automatic"* class translation process, although it is turned off by default. This can be devastatingly slow if improperly used, so the following is just a short list of things to keep in mind when using it. - Keep It (the object) Simple Stupid. (for exceptions, keep reading) - Do not require init params (for exceptions, keep reading) - Getter properties without setters could be dangerous (read: not tested) If any of the above are issues, use the `_serialize` method (see usage below). The server and client must **BOTH** have the `use_jsonclass` configuration item on and they must both have access to the same libraries used by the objects for this to work. If you have excessively nested arguments, it would be better to turn off the translation and manually invoke it on specific objects using `jsonrpclib.jsonclass.dump` / `jsonrpclib.jsonclass.load` (since the default behavior recursively goes through attributes and lists/dicts/tuples). * Sample file: `test_obj.py` ```python # This object is /very/ simple, and the system will look through the # attributes and serialize what it can. class TestObj(object): foo = 'bar' # This object requires __init__ params, so it uses the _serialize method # and returns a tuple of init params and attribute values (the init params # can be a dict or a list, but the attribute values must be a dict.) class TestSerial(object): foo = 'bar' def __init__(self, *args): self.args = args def _serialize(self): return (self.args, {'foo':self.foo,}) ``` - Sample usage: ```python >>> import jsonrpclib >>> import test_obj # History is used only to print the serialized form of beans >>> history = jsonrpclib.history.History() >>> testobj1 = test_obj.TestObj() >>> testobj2 = test_obj.TestSerial() >>> server = jsonrpclib.Server('http://localhost:8080', history=history) # The 'ping' just returns whatever is sent >>> ping1 = server.ping(testobj1) >>> ping2 = server.ping(testobj2) >>> print(history.request) {"id": "7805f1f9-9abd-49c6-81dc-dbd47229fe13", "jsonrpc": "2.0", "method": "ping", "params": [{"__jsonclass__": ["test_obj.TestSerial", []], "foo": "bar"} ]} >>> print(history.response) {"id": "7805f1f9-9abd-49c6-81dc-dbd47229fe13", "jsonrpc": "2.0", "result": {"__jsonclass__": ["test_obj.TestSerial", []], "foo": "bar"}} ``` This behavior is turned on by default. To deactivate it, just set the `use_jsonclass` member of a server `Config` to `False`. If you want to use a per-class serialization method, set its name in the `serialize_method` member of a server `Config`. Finally, if you are using classes that you have defined in the implementation (as in, not a separate library), you'll need to add those (on **BOTH** the server and the client) using the `config.classes.add()` method. Feedback on this "feature" is very, VERY much appreciated. ## Tests Tests are an almost-verbatim drop from the JSON-RPC specification 2.0 page. They can be run using *unittest* or *nosetest*: ``` python -m unittest discover tests python3 -m unittest discover tests nosetests tests ``` ## Why JSON-RPC? In my opinion, there are several reasons to choose JSON over XML for RPC: * Much simpler to read (I suppose this is opinion, but I know I'm right. :) * Size / Bandwidth - Main reason, a JSON object representation is just much smaller. * Parsing - JSON should be much quicker to parse than XML. * Easy class passing with `jsonclass` (when enabled) In the interest of being fair, there are also a few reasons to choose XML over JSON: * Your server doesn't do JSON (rather obvious) * Wider XML-RPC support across APIs (can we change this? :)) * Libraries are more established, *i.e.* more stable (Let's change this too) |
Commit History @HEAD
0
»»
- Switch to dh-sequence-*. Tristan Seligmann 3 years ago
- Bump Standards-Version to 4.5.0 (no changes). Tristan Seligmann 3 years ago
- Set Maintainer to DPMT (was uploader). Tristan Seligmann 3 years ago
- Declare rootless build. Tristan Seligmann 3 years ago
- Readme renamed. Tristan Seligmann 3 years ago
- Disable tests; they are not shipped. Tristan Seligmann 3 years ago
- New upstream release. Tristan Seligmann 3 years ago
- Update upstream source from tag 'upstream/0.4.1' Tristan Seligmann 3 years ago
- New upstream version 0.4.1 Tristan Seligmann 3 years ago
- Merge branch 'lintian-fixes' into 'master' Jelmer Vernooij 3 years ago
- Set upstream metadata fields: Bug-Database, Bug-Submit, Repository, Repository-Browse. Debian Janitor 3 years ago
- Bump debhelper from deprecated 9 to 12. Debian Janitor 3 years ago
- Use debhelper-compat instead of debian/compat Ondřej Nový 4 years ago
- Remove trailing whitespaces. Chris Lamb 5 years ago
- Convert git repository from git-dpm to gbp layout Ondřej Nový 5 years ago
- d/control: Set Vcs-* to salsa.debian.org Ondřej Nový 6 years ago
- Mention common-licenses. Tristan Seligmann 6 years ago
- Fix license name. Tristan Seligmann 6 years ago
- Add bug number to changelog. Tristan Seligmann 6 years ago
- Misc fixes. Tristan Seligmann 6 years ago
- Fix up copyright. Tristan Seligmann 6 years ago
- add initial Debian packaging Tristan Seligmann 6 years ago
- Initialize git-dpm Tristan Seligmann 6 years ago
- import jsonrpclib-pelix_0.3.1.orig.tar.gz Tristan Seligmann 6 years ago
0
»»