- Notifications
You must be signed in to change notification settings - Fork11
JSON-RPC 2.0 over ZeroMQ in Python
License
dwb/jsonrpc2-zeromq-python
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
Written byDan Brown. See the the LICENSE file for licensing information.
This is a library in Python enablingJSON-RPC 2.0 overZeroMQ. It includes support for both clients and servers.
This is packaged as a standard Python project, so just install usingpython setup.py install, or withpip.
Supports Python 2.7 and 3.3+.
from jsonrpc2_zeromq import RPCServerclass EchoServer(RPCServer): def handle_echo_method(self, msg): return msgs = EchoServer("tcp://127.0.0.1:57570")s.run()This creates a server listening on a ZeroMQ REP socket – so only methods are allowed, not notifications. See theRPCNotificationServer as well, which will listen on a ROUTER socket and allow notifications.
Each server is a PythonThread, so the call torun() can be replaced bystart() to have it running in a background thread.
from jsonrpc2_zeromq import RPCClientc = RPCClient("tcp://127.0.0.1:57570")print c.echo("Echo?")# Assuming the above compliant server, should print "Echo?"There are various classes, assuming different JSON-RPC 2.0 and ZeroMQ characteristics. The above, for example, will connect a REQ socket to the given endpoint.
Given a server that accepts notifications:
from jsonrpc2_zeromq import RPCNotificationServerclass EventReceiver(RPCNotificationServer): def handle_event_method(self, event_type, event_data): print "Got event!\nType: {0}\nData: {1}\n".format(event_type, event_data)s = EventReceiver("tcp://127.0.0.1:60666")s.run()You can then send notifications thus:
from jsonrpc2_zeromq import RPCNotifierClientc = RPCNotifierClient("tcp://127.0.0.1:60666")c.notify.event('birthday!', 'yours!')Also included areNotificationOnlyPullServer andNotifierOnlyPushClient which are designed for sending only notifications one-way over PUSH and PULL sockets.
There is also a client,NotificationReceiverClient, that is able to handle notifications returned back to it from a server. This is useful for situations where you "subscribe", via a standard RPC call, to events from the server, and they are returned back to the client as notifications when they occur. There is not currently a corresponding server class for this pattern. Here is a (one-sided) example:
from jsonrpc2_zeromq import NotificationReceiverClientclass EventSubscriber(NotificationReceiverClient): def handle_event_notification(self, event_type, event_data): print "Got event!\nType: {0}\nData: {1}\n".format(event_type, event_data)c = EventSubscriber("tcp://127.0.0.1:60666")c.subscribe()c.wait_for_notifications()Thestandard Python logging module is used for logging. It doesn't output anything by default. Either retrieve the built-in library logger withlogging.getLogger('jsonrpc2_zeromq') or pass your ownLogger instance into a client or server's__init__ with thelogger keyword argument.
Currently there are some helpful messages outputted at theDEBUG level, server exceptions onERROR, and a server start message onINFO.
Tests are included. Runpython setup.py test in the project root.
- 2.0
- Python 3.3+ support
- 1.1.2
- Allow newer (v14) pyzmq.
- Don't raise EINTR in server recv loop.
About
JSON-RPC 2.0 over ZeroMQ in Python
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors3
Uh oh!
There was an error while loading.Please reload this page.