diff options
author | 2015-08-24 13:22:48 +0300 | |
---|---|---|
committer | 2015-08-24 13:22:48 +0300 | |
commit | dab741a80699f86e86c91718872a052cca9bbb25 (patch) | |
tree | 1959c4a2cea440170a5113dcb067796cb20ffb64 /external_libs/python/pyzmq-14.7.0/examples/mongodb/client.py | |
parent | d3f26ece7d4383df0b22fe9c3cb3e695381ec737 (diff) |
Fixed dependencies of Control Plane to use external_lib sources
Diffstat (limited to 'external_libs/python/pyzmq-14.7.0/examples/mongodb/client.py')
-rw-r--r-- | external_libs/python/pyzmq-14.7.0/examples/mongodb/client.py | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/external_libs/python/pyzmq-14.7.0/examples/mongodb/client.py b/external_libs/python/pyzmq-14.7.0/examples/mongodb/client.py new file mode 100644 index 00000000..839dce71 --- /dev/null +++ b/external_libs/python/pyzmq-14.7.0/examples/mongodb/client.py @@ -0,0 +1,46 @@ +#----------------------------------------------------------------------------- +# Copyright (c) 2010 Justin Riley +# +# Distributed under the terms of the New BSD License. The full license is in +# the file COPYING.BSD, distributed as part of this software. +#----------------------------------------------------------------------------- + +import json +import zmq + +class MongoZMQClient(object): + """ + Client that connects with MongoZMQ server to add/fetch docs + """ + + def __init__(self, connect_addr='tcp://127.0.0.1:5000'): + self._context = zmq.Context() + self._socket = self._context.socket(zmq.DEALER) + self._socket.connect(connect_addr) + + def _send_recv_msg(self, msg): + self._socket.send_multipart(msg) + return self._socket.recv_multipart()[0] + + def get_doc(self, keys): + msg = ['get', json.dumps(keys)] + json_str = self._send_recv_msg(msg) + return json.loads(json_str) + + def add_doc(self, doc): + msg = ['add', json.dumps(doc)] + return self._send_recv_msg(msg) + +def main(): + client = MongoZMQClient() + for i in range(10): + doc = {'job': str(i)} + print "Adding doc", doc + print client.add_doc(doc) + for i in range(10): + query = {'job': str(i)} + print "Getting doc matching query:", query + print client.get_doc(query) + +if __name__ == "__main__": + main() |