Skip to main content

Enhanced version of python-jsonrpc for use with Bitcoin, for async use.

Project description

python-bitcoinrpc-async

python-bitcoinrpc-async adds asyncronous functionality to the AuthServiceProxy. It includes asyncio compatible methods for making and handling requests, making it easy to integrate into your codebase. With python-bitcoin-rpc you can now take advantage of the benefits of asynchronous programming, using the modern async and await syntax.

AuthServiceProxy

AuthServiceProxy is an asyncio compatible version of python-bitcoinrpc.

It includes the following features:

  • Asynchronous context manager for performing asyncronous HTTP requests
  • 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.

Installation

pip install python-bitcoinrpc-async

Example

from bitcoinrpcasync import AuthServiceProxy, JSONRPCException
import asyncio

async def main():
    async with AuthServiceProxy(f"http://{rpc_user}:{rpc_password}@127.0.0.1:8332") as rpc_connection:
        best_block_hash = await rpc_connection.getbestblockhash()
        print(best_block_hash)

loop = asyncio.get_event_loop()
loop.run_until_complete(main())

Batch support

from bitcoinrpcasync import AuthServiceProxy, JSONRPCException
import asyncio

async def main():
    async with AuthServiceProxy(f"http://{rpc_user}:{rpc_password}@127.0.0.1:8332") as rpc_connection:
        commands = [["getblockhash", height] for height in range(100)]
        block_hashes = await rpc_connection.batch_(commands)
        blocks = await rpc_connection.batch_([["getblock", h] for h in block_hashes])
        block_times = [block["time"] for block in blocks]
        print(block_times)

loop = asyncio.get_event_loop()
loop.run_until_complete(main())

Logging all RPC calls to stderr

from bitcoinrpcasync import AuthServiceProxy, JSONRPCException
import logging
import asyncio

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

async def main():
    async with AuthServiceProxy(f"http://{rpc_user}:{rpc_password}@127.0.0.1:8332") as rpc_connection:
        info = await rpc_connection.getinfo()
        print(info)

loop = asyncio.get_event_loop()
loop.run_until_complete(main())

Produces output on stderr like

DEBUG:BitcoinRPC:-1-> getinfo []
DEBUG:BitcoinRPC:<-1- {"connections": 8, ...etc }

Socket timeouts under heavy load

Pass the timeout argument to prevent "socket timed out" exceptions:

AuthServiceProxy(f"http://{rpc_user}:{rpc_password}@127.0.0.1:8332", timeout=120)

SSL/TLS connections

To set up a SSL/TLS connection pass a ssl.SSLContext object into the ssl_context default parameter of the AuthServiceProxy:

AuthServiceProxy(f"http://{rpc_user}:{rpc_password}@127.0.0.1:8332", ssl_context=ssl.create_default_context())

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

python-bitcoinrpc-async-1.0.0.tar.gz (27.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

python_bitcoinrpc_async-1.0.0-py3-none-any.whl (24.7 kB view details)

Uploaded Python 3

File details

Details for the file python-bitcoinrpc-async-1.0.0.tar.gz.

File metadata

  • Download URL: python-bitcoinrpc-async-1.0.0.tar.gz
  • Upload date:
  • Size: 27.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.4

File hashes

Hashes for python-bitcoinrpc-async-1.0.0.tar.gz
Algorithm Hash digest
SHA256 5f8b5302296f422cd26f02f659316979cba31709ca5c2fee60a63f0a62c0a57b
MD5 f28bc00ed566627ac9c7aee70042ae8e
BLAKE2b-256 ac2269490e604b507deb59e0c78336ce4ad1e4d8cb5aa6cc2d1617a24f0f7135

See more details on using hashes here.

File details

Details for the file python_bitcoinrpc_async-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for python_bitcoinrpc_async-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2eaef1e4a564a003635b95c1d6445a3f58a6739168d501a9447228b16954c111
MD5 89485e94b1667d5e28ba400f3205d2ae
BLAKE2b-256 f678ac752654d24a4f5b44bbdca86eed96dc2fe4fe2643ae28813e84afeda453

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page