forked from odasatoshi/jubatus_distributed_handson
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsonclient.py
36 lines (28 loc) · 1.21 KB
/
jsonclient.py
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
# jsonclient.py
# Simple JSONRPC client library created to work with Go servers
# Works with both Python 2.6+ and Python 3
# Copyright (c) 2011 Stephen Day, Bruce Eckel
# Distributed under the MIT Open-Source License:
# http://www.opensource.org/licenses/MIT
import json, socket, itertools
class JSONClient(object):
def __init__(self, addr):
self.socket = socket.create_connection(addr)
self.id_counter = itertools.count()
def __del__(self):
self.socket.close()
def call(self, name, *params):
request = dict(id=next(self.id_counter),
params=list(params),
method=name)
self.socket.sendall(json.dumps(request).encode())
# This must loop if resp is bigger than 4K
response = self.socket.recv(4096)
response = json.loads(response.decode())
if response.get('id') != request.get('id'):
raise Exception("expected id=%s, received id=%s: %s"
%(request.get('id'), response.get('id'),
response.get('error')))
if response.get('error') is not None:
raise Exception(response.get('error'))
return response.get('result')