Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions .gitignore

This file was deleted.

504 changes: 504 additions & 0 deletions LICENSE

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
include README.rst
include LICENSE
include MANIFEST.in
53 changes: 34 additions & 19 deletions README → README.rst
Original file line number Diff line number Diff line change
@@ -1,34 +1,46 @@
=================
python-bitcoinrpc
=================

AuthServiceProxy is an improved version of python-jsonrpc.

It includes the following generic improvements:

- HTTP connections persist for the life of the AuthServiceProxy object
- sends protocol 'version', per JSON-RPC 1.1
- sends proper, incrementing 'id'
- uses standard Python json lib
- can optionally log all RPC calls and results
- JSON-2.0 batch support
* HTTP connections persist for the life of the AuthServiceProxy object
* sends protocol 'version', per JSON-RPC 1.1
* sends proper, incrementing 'id'
* uses standard Python json lib
* can optionally log all RPC calls and results
* JSON-2.0 batch support

It also includes the following bitcoin-specific details:

- sends Basic HTTP authentication headers
- parses all JSON numbers that look like floats as Decimal,
and serializes Decimal values to JSON-RPC connections.
* sends Basic HTTP authentication headers
* parses all JSON numbers that look like floats as Decimal, and serializes Decimal values to JSON-RPC connections.

Installation
============

Installation:
1. change the first line of setup.py to point to the directory of your installation of Python
2. run setup.py

- change the first line of setup.py to point to the directory of your installation of python 2.*
- run setup.py
**Note**: *This will only install bitcoinrpc. If you also want to install jsonrpc to preserve backwards compatibility, you have to replace 'bitcoinrpc' with 'jsonrpc' in setup.py and run it again.*

Note: This will only install bitcoinrpc. If you also want to install jsonrpc to preserve
backwards compatibility, you have to replace 'bitcoinrpc' with 'jsonrpc' in setup.py and run it again.
Or simply install the library using pip::

Example usage:
pip install python-bitcoinrpc

Example Usage
=============
.. code:: python

from bitcoinrpc.authproxy import AuthServiceProxy, JSONRPCException

# rpc_user and rpc_password are set in the bitcoin.conf file
rpc_connection = AuthServiceProxy("http://%s:%s@127.0.0.1:8332"%(rpc_user, rpc_password))q
rpc_connection = AuthServiceProxy("http://%s:%s@127.0.0.1:8332" %
(rpc_user, rpc_password))

# hash of the best (tip) block in the longest block chain.
best_block_hash = rpc_connection.getbestblockhash()
print(rpc_connection.getblock(best_block_hash))

Expand All @@ -39,18 +51,21 @@ Example usage:
block_times = [ block["time"] for block in blocks ]
print(block_times)

Logging all RPC calls to stderr:
Logging all RPC calls to stderr
===============================
.. code:: python

from bitcoinrpc.authproxy import AuthServiceProxy, JSONRPCException
import logging

logging.basicConfig()
logging.getLogger("BitcoinRPC").setLevel(logging.DEBUG)

rpc_connection = AuthServiceProxy("http://%s:%s@127.0.0.1:8332"%(rpc_user, rpc_password))
rpc_connection = AuthServiceProxy("http://%s:%s@127.0.0.1:8332" %
(rpc_user, rpc_password))
print(rpc_connection.getinfo())

Produces output on stderr like:
Produces output on stderr like::

DEBUG:BitcoinRPC:-1-> getinfo []
DEBUG:BitcoinRPC:<-1- {"connections": 8, ...etc }
2 changes: 0 additions & 2 deletions bitcoinrpc/.gitignore

This file was deleted.

21 changes: 15 additions & 6 deletions bitcoinrpc/authproxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import decimal
import json
import logging
import sys
try:
import urllib.parse as urlparse
except ImportError:
Expand All @@ -61,7 +62,7 @@ def __init__(self, rpc_error):

def EncodeDecimal(o):
if isinstance(o, decimal.Decimal):
return round(o, 8)
return float(round(o, 8))
raise TypeError(repr(o) + " is not JSON serializable")

class AuthServiceProxy(object):
Expand Down Expand Up @@ -91,12 +92,20 @@ def __init__(self, service_url, service_name=None, timeout=HTTP_TIMEOUT, connect
# 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,
None, None, False,
timeout)
if sys.version_info < (3, 4):
self.__conn = httplib.HTTPSConnection(self.__url.hostname,
port, None, None, False,
timeout)
else:
self.__conn = httplib.HTTPSConnection(self.__url.hostname, port,
None, None, timeout)
else:
self.__conn = httplib.HTTPConnection(self.__url.hostname, port,
False, timeout)
if sys.version_info < (3, 4):
self.__conn = httplib.HTTPConnection(self.__url.hostname, port,
False, timeout)
else:
self.__conn = httplib.HTTPConnection(self.__url.hostname, port,
timeout)

def __getattr__(self, name):
if name.startswith('__') and name.endswith('__'):
Expand Down
33 changes: 22 additions & 11 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,25 @@

from distutils.core import setup

setup(name='python-bitcoinrpc',
version='0.1',
description='Enhanced version of python-jsonrpc for use with Bitcoin',
long_description=open('README').read(),
author='Jeff Garzik',
author_email='<jgarzik@exmulti.com>',
maintainer='Jeff Garzik',
maintainer_email='<jgarzik@exmulti.com>',
url='http://www.github.com/jgarzik/python-bitcoinrpc',
packages=['bitcoinrpc'],
classifiers=['License :: OSI Approved :: GNU Library or Lesser General Public License (LGPL)', 'Operating System :: OS Independent'])
setup(
name='python-bitcoinrpc',
version='0.1',
description='Enhanced version of python-jsonrpc for use with Bitcoin',
long_description=open('README.rst').read(),
author='Jeff Garzik',
author_email='<jgarzik@exmulti.com>',
maintainer='Jeff Garzik',
maintainer_email='<jgarzik@exmulti.com>',
url='http://www.github.com/jgarzik/python-bitcoinrpc',
packages=['bitcoinrpc'],
license='LGPL-2.1+',
classifiers=[
'Intended Audience :: Developers',
'License :: OSI Approved :: GNU Library or Lesser General Public License (LGPL)',
'Operating System :: OS Independent',
'Programming Language :: Python',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3.3',
'Programming Language :: Python :: 3.4',
]
)