|
|
@ -39,8 +39,9 @@ try: |
|
|
|
except ImportError: |
|
|
|
except ImportError: |
|
|
|
import httplib |
|
|
|
import httplib |
|
|
|
import base64 |
|
|
|
import base64 |
|
|
|
import json |
|
|
|
|
|
|
|
import decimal |
|
|
|
import decimal |
|
|
|
|
|
|
|
import json |
|
|
|
|
|
|
|
import logging |
|
|
|
try: |
|
|
|
try: |
|
|
|
import urllib.parse as urlparse |
|
|
|
import urllib.parse as urlparse |
|
|
|
except ImportError: |
|
|
|
except ImportError: |
|
|
@ -50,6 +51,7 @@ USER_AGENT = "AuthServiceProxy/0.1" |
|
|
|
|
|
|
|
|
|
|
|
HTTP_TIMEOUT = 30 |
|
|
|
HTTP_TIMEOUT = 30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
log = logging.getLogger("BitcoinRPC") |
|
|
|
|
|
|
|
|
|
|
|
class JSONRPCException(Exception): |
|
|
|
class JSONRPCException(Exception): |
|
|
|
def __init__(self, rpc_error): |
|
|
|
def __init__(self, rpc_error): |
|
|
@ -57,7 +59,14 @@ class JSONRPCException(Exception): |
|
|
|
self.error = rpc_error |
|
|
|
self.error = rpc_error |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def EncodeDecimal(o): |
|
|
|
|
|
|
|
if isinstance(o, decimal.Decimal): |
|
|
|
|
|
|
|
return round(o, 8) |
|
|
|
|
|
|
|
raise TypeError(repr(o) + " is not JSON serializable") |
|
|
|
|
|
|
|
|
|
|
|
class AuthServiceProxy(object): |
|
|
|
class AuthServiceProxy(object): |
|
|
|
|
|
|
|
__id_count = 0 |
|
|
|
|
|
|
|
|
|
|
|
def __init__(self, service_url, service_name=None, timeout=HTTP_TIMEOUT, connection=None): |
|
|
|
def __init__(self, service_url, service_name=None, timeout=HTTP_TIMEOUT, connection=None): |
|
|
|
self.__service_url = service_url |
|
|
|
self.__service_url = service_url |
|
|
|
self.__service_name = service_name |
|
|
|
self.__service_name = service_name |
|
|
@ -66,7 +75,6 @@ class AuthServiceProxy(object): |
|
|
|
port = 80 |
|
|
|
port = 80 |
|
|
|
else: |
|
|
|
else: |
|
|
|
port = self.__url.port |
|
|
|
port = self.__url.port |
|
|
|
self.__id_count = 0 |
|
|
|
|
|
|
|
(user, passwd) = (self.__url.username, self.__url.password) |
|
|
|
(user, passwd) = (self.__url.username, self.__url.password) |
|
|
|
try: |
|
|
|
try: |
|
|
|
user = user.encode('utf8') |
|
|
|
user = user.encode('utf8') |
|
|
@ -99,12 +107,14 @@ class AuthServiceProxy(object): |
|
|
|
return AuthServiceProxy(self.__service_url, name, connection=self.__conn) |
|
|
|
return AuthServiceProxy(self.__service_url, name, connection=self.__conn) |
|
|
|
|
|
|
|
|
|
|
|
def __call__(self, *args): |
|
|
|
def __call__(self, *args): |
|
|
|
self.__id_count += 1 |
|
|
|
AuthServiceProxy.__id_count += 1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
log.debug("-%s-> %s %s"%(AuthServiceProxy.__id_count, self.__service_name, |
|
|
|
|
|
|
|
json.dumps(args, default=EncodeDecimal))) |
|
|
|
postdata = json.dumps({'version': '1.1', |
|
|
|
postdata = json.dumps({'version': '1.1', |
|
|
|
'method': self.__service_name, |
|
|
|
'method': self.__service_name, |
|
|
|
'params': args, |
|
|
|
'params': args, |
|
|
|
'id': self.__id_count}) |
|
|
|
'id': AuthServiceProxy.__id_count}, default=EncodeDecimal) |
|
|
|
self.__conn.request('POST', self.__url.path, postdata, |
|
|
|
self.__conn.request('POST', self.__url.path, postdata, |
|
|
|
{'Host': self.__url.hostname, |
|
|
|
{'Host': self.__url.hostname, |
|
|
|
'User-Agent': USER_AGENT, |
|
|
|
'User-Agent': USER_AGENT, |
|
|
@ -121,7 +131,8 @@ class AuthServiceProxy(object): |
|
|
|
return response['result'] |
|
|
|
return response['result'] |
|
|
|
|
|
|
|
|
|
|
|
def _batch(self, rpc_call_list): |
|
|
|
def _batch(self, rpc_call_list): |
|
|
|
postdata = json.dumps(list(rpc_call_list)) |
|
|
|
postdata = json.dumps(list(rpc_call_list), default=EncodeDecimal) |
|
|
|
|
|
|
|
log.debug("--> "+postdata) |
|
|
|
self.__conn.request('POST', self.__url.path, postdata, |
|
|
|
self.__conn.request('POST', self.__url.path, postdata, |
|
|
|
{'Host': self.__url.hostname, |
|
|
|
{'Host': self.__url.hostname, |
|
|
|
'User-Agent': USER_AGENT, |
|
|
|
'User-Agent': USER_AGENT, |
|
|
@ -136,5 +147,10 @@ class AuthServiceProxy(object): |
|
|
|
raise JSONRPCException({ |
|
|
|
raise JSONRPCException({ |
|
|
|
'code': -342, 'message': 'missing HTTP response from server'}) |
|
|
|
'code': -342, 'message': 'missing HTTP response from server'}) |
|
|
|
|
|
|
|
|
|
|
|
return json.loads(http_response.read().decode('utf8'), |
|
|
|
responsedata = http_response.read().decode('utf8') |
|
|
|
parse_float=decimal.Decimal) |
|
|
|
response = json.loads(responsedata, parse_float=decimal.Decimal) |
|
|
|
|
|
|
|
if "error" in response and response["error"] is None: |
|
|
|
|
|
|
|
log.debug("<-%s- %s"%(response["id"], json.dumps(response["result"], default=EncodeDecimal))) |
|
|
|
|
|
|
|
else: |
|
|
|
|
|
|
|
log.debug("<-- "+responsedata) |
|
|
|
|
|
|
|
return response |
|
|
|