-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add protocol/framework examples on one page.
- Loading branch information
Showing
2 changed files
with
82 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
.. rubric:: :doc:`index` | ||
|
||
jsonrpcclient Examples | ||
********************** | ||
|
||
Sending JSON-RPC requests in Python using various frameworks and transport | ||
protocols. | ||
|
||
Requests | ||
======== | ||
|
||
Uses Kenneth Reitz's `Requests <http://docs.python-requests.org/>`__ | ||
library. | ||
|
||
:: | ||
|
||
$ pip install jsonrpcclient requests | ||
|
||
:: | ||
|
||
>>> from jsonrpcclient.http_client import HTTPClient | ||
>>> HTTPClient('http://localhost:5000/').request('ping') | ||
--> {"jsonrpc": "2.0", "method": "ping", "id": 1} | ||
<-- {"jsonrpc": "2.0", "result": "pong", "id": 1} | ||
'pong' | ||
|
||
ZeroMQ | ||
====== | ||
|
||
Uses `pyzmq <https://pyzmq.readthedocs.io/>`__. | ||
|
||
:: | ||
|
||
$ pip install jsonrpcclient pyzmq | ||
|
||
:: | ||
|
||
>>> from jsonrpcclient.zmq_client import ZMQClient | ||
>>> ZMQClient('tcp://localhost:5000').request('ping') | ||
--> {"jsonrpc": "2.0", "method": "ping", "id": 1} | ||
<-- {"jsonrpc": "2.0", "result": "pong", "id": 1} | ||
'pong' | ||
|
||
Tornado | ||
======= | ||
|
||
`Tornado <http://www.tornadoweb.org/>`__ users can send an asynchronous | ||
request. | ||
|
||
:: | ||
|
||
$ pip install jsonrpcclient tornado | ||
|
||
:: | ||
|
||
from tornado import gen, ioloop | ||
from jsonrpcclient.tornado_client import TornadoClient | ||
|
||
client = TornadoClient('http://localhost:5000/') | ||
|
||
def done_callback(future): | ||
print(future.result()) | ||
|
||
@gen.coroutine | ||
def main(): | ||
future = client.request('ping') | ||
future.add_done_callback(done_callback) | ||
yield(future) | ||
|
||
io_loop = ioloop.IOLoop.current().run_sync(main) | ||
|
||
:: | ||
|
||
$ python client.py | ||
INFO:jsonrpcclient.client.request:{"jsonrpc": "2.0", "method": "ping", "id": 1} | ||
INFO:jsonrpcclient.client.response:{"jsonrpc": "2.0", "result": "pong", "id": 1} | ||
pong |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5fa1f84
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note that pip supports this syntax for extra deps:
pip install jsonrpcclient[requests]
.requests
here is key inextras_require
insetup.py
.5fa1f84
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Created issue #29.