2017-08-29 04:21:32 +00:00
|
|
|
import json
|
|
|
|
import urllib2
|
|
|
|
|
2018-05-07 01:45:56 +00:00
|
|
|
|
|
|
|
def request(action, params={}, version=5):
|
|
|
|
return {'action': action, 'params': params, 'version': version}
|
|
|
|
|
|
|
|
|
|
|
|
def invoke(action, params={}, version=5, url='http://localhost:8765'):
|
|
|
|
requestJson = json.dumps(request(action, params, version))
|
|
|
|
response = json.load(urllib2.urlopen(urllib2.Request(url, requestJson)))
|
2018-05-07 02:01:24 +00:00
|
|
|
if len(response) != 2:
|
|
|
|
raise Exception('response has an unexpected number of fields')
|
|
|
|
if 'error' not in response:
|
|
|
|
raise Exception('response is missing required error field')
|
|
|
|
if 'result' not in response:
|
|
|
|
raise Exception('response is missing required result field')
|
|
|
|
if response['error'] is not None:
|
|
|
|
raise Exception(response['error'])
|
|
|
|
return response['result']
|