From 3287f4a76b6390150f336a6d6ed8fb3022918f2e Mon Sep 17 00:00:00 2001 From: Daniel Fiala Date: Wed, 26 Apr 2017 14:08:39 +0200 Subject: [PATCH 1/3] Authproxy reimplemented with asyncio. First attempt. --- bitcoinrpc/asyncio/__init__.py | 0 bitcoinrpc/asyncio/authproxy.py | 192 ++++++++++++++++++++++++++++++++ 2 files changed, 192 insertions(+) create mode 100644 bitcoinrpc/asyncio/__init__.py create mode 100644 bitcoinrpc/asyncio/authproxy.py diff --git a/bitcoinrpc/asyncio/__init__.py b/bitcoinrpc/asyncio/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/bitcoinrpc/asyncio/authproxy.py b/bitcoinrpc/asyncio/authproxy.py new file mode 100644 index 0000000..d63eb60 --- /dev/null +++ b/bitcoinrpc/asyncio/authproxy.py @@ -0,0 +1,192 @@ + +""" + Copyright 2011 Jeff Garzik + + AuthServiceProxy has the following improvements over python-jsonrpc's + ServiceProxy class: + + - HTTP connections persist for the life of the AuthServiceProxy object + (if server supports HTTP/1.1) + - sends protocol 'version', per JSON-RPC 1.1 + - sends proper, incrementing 'id' + - sends Basic HTTP authentication headers + - parses all JSON numbers that look like floats as Decimal + - uses standard Python json lib + + Previous copyright, from python-jsonrpc/jsonrpc/proxy.py: + + Copyright (c) 2007 Jan-Klaas Kollhof + + This file is part of jsonrpc. + + jsonrpc is free software; you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation; either version 2.1 of the License, or + (at your option) any later version. + + This software is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with this software; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +""" + +import yieldfrom.http.client as httplib +import base64 +import decimal +import json +import logging +try: + import urllib.parse as urlparse +except ImportError: + import urlparse + +USER_AGENT = "AuthServiceProxy/0.1" + +HTTP_TIMEOUT = 30 + +log = logging.getLogger("BitcoinRPC") + +class JSONRPCException(Exception): + def __init__(self, rpc_error): + parent_args = [] + try: + parent_args.append(rpc_error['message']) + except: + pass + Exception.__init__(self, *parent_args) + self.error = rpc_error + self.code = rpc_error['code'] if 'code' in rpc_error else None + self.message = rpc_error['message'] if 'message' in rpc_error else None + + def __str__(self): + return '%d: %s' % (self.code, self.message) + + def __repr__(self): + return '<%s \'%s\'>' % (self.__class__.__name__, self) + + +def EncodeDecimal(o): + if isinstance(o, decimal.Decimal): + return float(round(o, 8)) + raise TypeError(repr(o) + " is not JSON serializable") + +class AuthServiceProxy(object): + __id_count = 0 + + async def __init__(self, service_url, service_name=None, timeout=HTTP_TIMEOUT, connection=None): + self.__service_url = service_url + self.__service_name = service_name + self.__url = urlparse.urlparse(service_url) + if self.__url.port is None: + port = 80 + else: + port = self.__url.port + (user, passwd) = (self.__url.username, self.__url.password) + try: + user = user.encode('utf8') + except AttributeError: + pass + try: + passwd = passwd.encode('utf8') + except AttributeError: + pass + authpair = user + b':' + passwd + self.__auth_header = b'Basic ' + base64.b64encode(authpair) + + self.__timeout = timeout + + if connection: + # Callables re-use the connection of the original proxy + self.__conn = connection + elif self.__url.scheme == 'https': + self.__conn = httplib.HTTPSConnection(self.__url.hostname, port, + timeout=timeout) + else: + self.__conn = httplib.HTTPConnection(self.__url.hostname, port, + timeout=timeout) + + def __getattr__(self, name): + if name.startswith('__') and name.endswith('__'): + # Python internal stuff + raise AttributeError + if self.__service_name is not None: + name = "%s.%s" % (self.__service_name, name) + return AuthServiceProxy(self.__service_url, name, self.__timeout, self.__conn) + + async def __call__(self, *args): + 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', + 'method': self.__service_name, + 'params': args, + 'id': AuthServiceProxy.__id_count}, default=EncodeDecimal) + await self.__conn.request('POST', self.__url.path, postdata, + {'Host': self.__url.hostname, + 'User-Agent': USER_AGENT, + 'Authorization': self.__auth_header, + 'Content-type': 'application/json'}) + self.__conn.sock.settimeout(self.__timeout) + + response = await self._get_response() + if response.get('error') is not None: + raise JSONRPCException(response['error']) + elif 'result' not in response: + raise JSONRPCException({ + 'code': -343, 'message': 'missing JSON-RPC result'}) + + return response['result'] + + async def batch_(self, rpc_calls): + """Batch RPC call. + Pass array of arrays: [ [ "method", params... ], ... ] + Returns array of results. + """ + batch_data = [] + for rpc_call in rpc_calls: + AuthServiceProxy.__id_count += 1 + m = rpc_call.pop(0) + batch_data.append({"jsonrpc":"2.0", "method":m, "params":rpc_call, "id":AuthServiceProxy.__id_count}) + + postdata = json.dumps(batch_data, default=EncodeDecimal) + log.debug("--> "+postdata) + await self.__conn.request('POST', self.__url.path, postdata, + {'Host': self.__url.hostname, + 'User-Agent': USER_AGENT, + 'Authorization': self.__auth_header, + 'Content-type': 'application/json'}) + results = [] + responses = await self._get_response() + for response in responses: + if response['error'] is not None: + raise JSONRPCException(response['error']) + elif 'result' not in response: + raise JSONRPCException({ + 'code': -343, 'message': 'missing JSON-RPC result'}) + else: + results.append(response['result']) + return results + + async def _get_response(self): + http_response = await self.__conn.getresponse() + if http_response is None: + raise JSONRPCException({ + 'code': -342, 'message': 'missing HTTP response from server'}) + + content_type = http_response.getheader('Content-Type') + if content_type != 'application/json': + raise JSONRPCException({ + 'code': -342, 'message': 'non-JSON HTTP response with \'%i %s\' from server' % (http_response.status, http_response.reason)}) + + responsedata = await http_response.read().decode('utf8') + 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 From e7659b79bcc8c517cbd77b0e1cf6eaa783d94a6c Mon Sep 17 00:00:00 2001 From: Daniel Fiala Date: Wed, 26 Apr 2017 18:54:37 +0200 Subject: [PATCH 2/3] Fixed mistakes in async impl. --- bitcoinrpc/asyncio/authproxy.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/bitcoinrpc/asyncio/authproxy.py b/bitcoinrpc/asyncio/authproxy.py index d63eb60..ac182c6 100644 --- a/bitcoinrpc/asyncio/authproxy.py +++ b/bitcoinrpc/asyncio/authproxy.py @@ -77,7 +77,7 @@ def EncodeDecimal(o): class AuthServiceProxy(object): __id_count = 0 - async 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_name = service_name self.__url = urlparse.urlparse(service_url) @@ -131,7 +131,7 @@ async def __call__(self, *args): 'User-Agent': USER_AGENT, 'Authorization': self.__auth_header, 'Content-type': 'application/json'}) - self.__conn.sock.settimeout(self.__timeout) + #self.__conn.sock.settimeout(self.__timeout) response = await self._get_response() if response.get('error') is not None: @@ -183,7 +183,8 @@ async def _get_response(self): raise JSONRPCException({ 'code': -342, 'message': 'non-JSON HTTP response with \'%i %s\' from server' % (http_response.status, http_response.reason)}) - responsedata = await http_response.read().decode('utf8') + responsedata = await http_response.read() + responsedata = responsedata.decode('utf8') 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))) From 30069e64c182c9b09c75e0d9a1ed0ab9d5c09d21 Mon Sep 17 00:00:00 2001 From: Daniel Fiala Date: Sun, 30 Apr 2017 13:36:28 +0200 Subject: [PATCH 3/3] Added comment about timeouts in connection's sockets. --- bitcoinrpc/asyncio/authproxy.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/bitcoinrpc/asyncio/authproxy.py b/bitcoinrpc/asyncio/authproxy.py index ac182c6..cfa1ec5 100644 --- a/bitcoinrpc/asyncio/authproxy.py +++ b/bitcoinrpc/asyncio/authproxy.py @@ -131,7 +131,12 @@ async def __call__(self, *args): 'User-Agent': USER_AGENT, 'Authorization': self.__auth_header, 'Content-type': 'application/json'}) - #self.__conn.sock.settimeout(self.__timeout) + # It seems that yieldfrom.http.client takes care for timeout + # that is provided through __init__(.) . + # With asyncio, timeout can be also detected with: + # AbstractEventLoop.call_later() . + # So this call isn't needed! + # self.__conn.socket().settimeout(self.__timeout) response = await self._get_response() if response.get('error') is not None: