File: index.rst

package info (click to toggle)
python-tinyrpc 0.6-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 324 kB
  • sloc: python: 1,700; makefile: 142; sh: 16
file content (178 lines) | stat: -rw-r--r-- 4,070 bytes parent folder | download | duplicates (3)
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
tinyrpc: A modular RPC library
==============================

``tinyrpc`` is a library for making and handling RPC calls in python. Its
initial scope is handling jsonrpc_, although it aims to be very well-documented
and modular to make it easy to add support for further protocols.

A feature is support of multiple transports (or none at all) and providing
clever syntactic sugar for writing dispatchers.

Quickstart examples
-------------------

The source contains all of these examples in a working fashion in the examples
subfolder.

HTTP based
~~~~~~~~~~

A client making JSONRPC calls via HTTP (this requires :py:mod:`requests` to be
installed):

.. code-block:: python

   from tinyrpc.protocols.jsonrpc import JSONRPCProtocol
   from tinyrpc.transports.http import HttpPostClientTransport
   from tinyrpc import RPCClient

   rpc_client = RPCClient(
       JSONRPCProtocol(),
       HttpPostClientTransport('http://example.org/jsonrpc/2.0/')
   )

   time_server = rpc_client.get_proxy()

   # ...

   # call a method called 'get_time_in' with a single string argument
   time_in_berlin = time_server.get_time_in('Europe/Berlin')

These can be answered by a server implemented as follows:

.. code-block:: python

   import gevent
   import gevent.wsgi
   import gevent.queue
   from tinyrpc.protocols.jsonrpc import JSONRPCProtocol
   from tinyrpc.transports.wsgi import WsgiServerTransport
   from tinyrpc.server.gevent import RPCServerGreenlets
   from tinyrpc.dispatch import RPCDispatcher

   dispatcher = RPCDispatcher()
   transport = WsgiServerTransport(queue_class=gevent.queue.Queue)

   # start wsgi server as a background-greenlet
   wsgi_server = gevent.wsgi.WSGIServer(('127.0.0.1', 80), transport.handle)
   gevent.spawn(wsgi_server.serve_forever)

   rpc_server = RPCServerGreenlets(
       transport,
       JSONRPCProtocol(),
       dispatcher
   )

   @dispatcher.public
   def reverse_string(s):
       return s[::-1]

   # in the main greenlet, run our rpc_server
   rpc_server.serve_forever()


0mq
~~~

An example using :py:mod:`zmq` is very similiar, differing only in the
instantiation of the transport:

.. code-block:: python

  import zmq

  from tinyrpc.protocols.jsonrpc import JSONRPCProtocol
  from tinyrpc.transports.zmq import ZmqClientTransport
  from tinyrpc import RPCClient

  ctx = zmq.Context()

  rpc_client = RPCClient(
      JSONRPCProtocol(),
      ZmqClientTransport.create(ctx, 'tcp://127.0.0.1:5001')
  )

  remote_server = rpc_client.get_proxy()

  # call a method called 'reverse_string' with a single string argument
  result = remote_server.reverse_string('Hello, World!')

  print "Server answered:", result


Matching server:

.. code-block:: python

   import zmq

   from tinyrpc.protocols.jsonrpc import JSONRPCProtocol
   from tinyrpc.transports.zmq import ZmqServerTransport
   from tinyrpc.server import RPCServer
   from tinyrpc.dispatch import RPCDispatcher

   ctx = zmq.Context()
   dispatcher = RPCDispatcher()
   transport = ZmqServerTransport.create(ctx, 'tcp://127.0.0.1:5001')

   rpc_server = RPCServer(
       transport,
       JSONRPCProtocol(),
       dispatcher
   )

   @dispatcher.public
   def reverse_string(s):
       return s[::-1]

   rpc_server.serve_forever()



Further examples
----------------

In :doc:`protocols`, you can find client and server examples on how
to use just the protocol parsing parts of ``tinyrpc``.

The :py:class:`~tinyrpc.dispatch.RPCDispatcher` should be useful on its own (or
at least easily replaced with one of your choosing), see :doc:`dispatch` for
details.



Table of contents
-----------------

.. toctree::
   :maxdepth: 2

   structure
   protocols
   dispatch
   transports
   client
   server
   exceptions

People
------

Creator
~~~~~~~

- Marc Brinkmann: https://github.com/mbr

Maintainer
~~~~~~~~~~

- Leo Noordergraaf: https://github.com/lnoor

Contributors
~~~~~~~~~~~~

- Guilherme Salgado: https://github.com/gsalgado
- jnnk: https://github.com/jnnk
- Satoshi Kobayashi: https://github.com/satosi-k

.. _jsonrpc: http://jsonrpc.org