Bitcoin Forum
October 01, 2016, 12:05:57 PM *
News: Latest stable version of Bitcoin Core: 0.13.0 (New!) [Torrent]. Make sure you verify it.
 
   Home   Help Search Donate Login Register  
Pages: [1]
  Print  
Author Topic: Python wrapper for bitcoind  (Read 3212 times)
db
Sr. Member
****
Offline Offline

Activity: 279



View Profile
July 08, 2010, 11:50:43 PM
 #1

While creating the Bitcoin automation for mullvad.net I made a python wrapper around bitcoind. Should anyone else find such a thing useful it is available here:

https://www.mullvad.net/download/bitcoin.py

Usage example:

>>> import bitcoin
>>> bitcoin.getaddressesbylabel("foo")
['16au2i9ny5pnwCPnoekov2hHMhtS7qcFjR', '19BkB63mo3NQRPNeHkc3bWnKGBU7oL8Dp7']
>>> bitcoin.getreceivedbylabel("Your Address")
6.0
>>> bitcoin.listreceivedbyaddress()
{'16au2i9ny5pnwCPnoekov2hHMhtS7qcFjR': {'amount': 2000.0, 'label': 'foo', 'confirmations': 226, 'address': '16au2i9ny5pnwCPnoekov2hHMhtS7qcFjR'}, '1FLD8wCNNBh2wg2mnGjD9ogoLaRxpSz7TW': {'amount': 6.0, 'label': 'Your Address', 'confirmations': 343, 'address': '1FLD8wCNNBh2wg2mnGjD9ogoLaRxpSz7TW'}}
>>> bitcoin.sendtoaddress("1Nsq3itZULUZjtZGcjNrtZtwT8aMsHu1R1", 1234.5)
>>> bitcoin.sendtoaddress("1Nsq3itZULUZjtZGcjNrtZtwT8aMsHu1R1", 1000000)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "bitcoin.py", line 73, in sendtoaddress
    _cmd("sendtoaddress %s %d %s %s" % (bitcoinaddress, amount, comment, comment_to))
  File "bitcoin.py", line 86, in _cmd
    return _parse(_cmd_raw(args))[0]
  File "bitcoin.py", line 92, in _cmd_raw
    raise BitcoinError(errors)
bitcoin.BitcoinError: error: Insufficient funds

>>>

1475323557
Hero Member
*
Offline Offline

Posts: 1475323557

View Profile Personal Message (Offline)

Ignore
1475323557
Reply with quote  #2

1475323557
Report to moderator
1475323557
Hero Member
*
Offline Offline

Posts: 1475323557

View Profile Personal Message (Offline)

Ignore
1475323557
Reply with quote  #2

1475323557
Report to moderator
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction. Advertise here.
1475323557
Hero Member
*
Offline Offline

Posts: 1475323557

View Profile Personal Message (Offline)

Ignore
1475323557
Reply with quote  #2

1475323557
Report to moderator
1475323557
Hero Member
*
Offline Offline

Posts: 1475323557

View Profile Personal Message (Offline)

Ignore
1475323557
Reply with quote  #2

1475323557
Report to moderator
NewLibertyStandard
Sr. Member
****
Offline Offline

Activity: 252



View Profile WWW
July 09, 2010, 03:37:21 AM
 #2

That's great! Thanks db!

Treazant: A Fullever Rewarding Bitcoin - Backup Your Wallet TODAY to Double Your Money! - Dual Currency Donation Address: 1Dnvwj3hAGSwFPMnkJZvi3KnaqksRPa74p
phelix
Legendary
*
Offline Offline

Activity: 1666


nmc:id/phelix


View Profile
August 14, 2011, 09:45:28 AM
 #3

That's great! Thanks db!
+1

blockchained.com ■ bitcointalk top posts
etotheipi
Legendary
*
expert
Offline Offline

Activity: 1428


Core Armory Developer


View Profile WWW
August 14, 2011, 09:07:35 PM
 #4

Looks great, db.

Just a minor recommendation: given the ease with which you can catch errors in python, you might consider an except(BitcoinError) clause to gracefully fail when you have insufficient funds, or other error.   Your original post showed this:

Code:
>>> bitcoin.sendtoaddress("1Nsq3itZULUZjtZGcjNrtZtwT8aMsHu1R1", 1000000)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "bitcoin.py", line 73, in sendtoaddress
    _cmd("sendtoaddress %s %d %s %s" % (bitcoinaddress, amount, comment, comment_to))
  File "bitcoin.py", line 86, in _cmd
    return _parse(_cmd_raw(args))[0]
  File "bitcoin.py", line 92, in _cmd_raw
    raise BitcoinError(errors)
bitcoin.BitcoinError: error: Insufficient funds

It should be pretty easy to produce something like this, instead:

Code:
>>> bitcoin.sendtoaddress("1Nsq3itZULUZjtZGcjNrtZtwT8aMsHu1R1", 1000000)
***ERROR:  Insufficient funds***
   Attempted Transaction:  1000000
   Available Balance:       922100

Or whatever info/format-string you want, but those Traceback messages tend to scare users.

Founder and CEO of Armory Technologies, Inc.
Armory Bitcoin Wallet: Bringing cold storage to the average user!
Only use Armory software signed by the Armory Offline Signing Key (0x98832223)

Please donate to the Armory project by clicking here!    (or donate directly via 1QBDLYTDFHHZAABYSKGKPWKLSXZWCCJQBX -- yes, it's a real address!)
phelix
Legendary
*
Offline Offline

Activity: 1666


nmc:id/phelix


View Profile
August 14, 2011, 11:16:55 PM
 #5

[...]
Or whatever info/format-string you want, but those Traceback messages tend to scare users.

lol, for me it's the other way round.

it may be true but for me tracebacks are much more useful. custom error messages make finding bugs much harder and it can be a pain to reinstall the tracebacks.


blockchained.com ■ bitcointalk top posts
etotheipi
Legendary
*
expert
Offline Offline

Activity: 1428


Core Armory Developer


View Profile WWW
August 15, 2011, 12:13:46 AM
 #6

I'm differentiating users from developers, here.  I recognize the Tracebacks are useful for developers, but as a user, I don't plan to be debugging his code.  Plus, this isn't the result of a bug--it's a legitimate validation failure response.   Not a big deal, it was just a recommendation...


Founder and CEO of Armory Technologies, Inc.
Armory Bitcoin Wallet: Bringing cold storage to the average user!
Only use Armory software signed by the Armory Offline Signing Key (0x98832223)

Please donate to the Armory project by clicking here!    (or donate directly via 1QBDLYTDFHHZAABYSKGKPWKLSXZWCCJQBX -- yes, it's a real address!)
phelix
Legendary
*
Offline Offline

Activity: 1666


nmc:id/phelix


View Profile
April 08, 2012, 03:39:36 PM
 #7

fyi: namecoind wrapper forked: http://dot-bit.org/forum/viewtopic.php?f=2&t=482

blockchained.com ■ bitcointalk top posts
Pages: [1]
  Print  
 
Jump to:  

Sponsored by , a Bitcoin-accepting VPN.
Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!