Skip to content

Commit

Permalink
Add protocol/framework examples on one page.
Browse files Browse the repository at this point in the history
Re #17 Closes #23
  • Loading branch information
bcb committed Sep 13, 2016
1 parent afed770 commit 5fa1f84
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 3 deletions.
77 changes: 77 additions & 0 deletions doc/examples.rst
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
8 changes: 5 additions & 3 deletions doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ Send `JSON-RPC <http://www.jsonrpc.org/>`__ requests in Python 2.7 and 3.3+.
<-- {"jsonrpc": "2.0", "result": "meow", "id": 1}
'meow'

For advanced usage and configuration, see the :doc:`api`.
This example uses `Requests <http://docs.python-requests.org/>`__ library to
send a request. More options are demonstrated on the :doc:`examples <examples>`
page. For advanced usage and configuration, see the :doc:`api`.

Contribute on `Github <https://github.com/bcb/jsonrpcclient>`_.
Contribute on `Github <https://github.com/bcb/jsonrpcclient>`__.

See also: `jsonrpcserver.readthedocs.io <https://jsonrpcserver.readthedocs.io/>`_
See also: `jsonrpcserver.readthedocs.io <https://jsonrpcserver.readthedocs.io/>`__

2 comments on commit 5fa1f84

@saaj
Copy link
Contributor

@saaj saaj commented on 5fa1f84 Sep 19, 2016

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 in extras_require in setup.py.

@bcb
Copy link
Member Author

@bcb bcb commented on 5fa1f84 Sep 19, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Created issue #29.

Please sign in to comment.