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
|
import sys
import uuid
import json
import tnetstring
import zmq
def make_tnet_compat(obj):
if isinstance(obj, dict):
out = {}
for k, v in obj.items():
out[make_tnet_compat(k)] = make_tnet_compat(v)
return out
elif isinstance(obj, list):
out = list()
for v in obj:
out.append(make_tnet_compat(v))
return out
elif isinstance(obj, str):
return obj.encode('utf-8')
else:
return obj
ctx = zmq.Context()
sock = ctx.socket(zmq.REQ)
sock.connect(sys.argv[1])
method = sys.argv[2]
if len(sys.argv) > 3:
args = json.loads(sys.argv[3])
assert(isinstance(args, dict))
else:
args = {}
print('calling {}: args={}'.format(method, repr(args)))
req = {
b'id': str(uuid.uuid4()).encode('utf-8'),
b'method': method.encode('utf-8'),
b'args': make_tnet_compat(args)
}
sock.send(tnetstring.dumps(req))
resp = tnetstring.loads(sock.recv())
if resp[b'success']:
value = resp[b'value']
print('success: {}'.format(repr(value)))
else:
condition = resp[b'condition'].decode('utf-8')
value = resp.get(b'value')
print('error: {} {}'.format(condition, repr(value)))
|