Bitcoin Forum
November 19, 2024, 01:53:59 AM *
News: Check out the artwork 1Dq created to commemorate this forum's 15th anniversary
 
   Home   Help Search Login Register More  
Pages: [1] 2 3 »  All
  Print  
Author Topic: Python scripting  (Read 2580 times)
donGeilo (OP)
Full Member
***
Offline Offline

Activity: 169
Merit: 100



View Profile
August 17, 2016, 06:52:09 PM
 #1

Hi, i'm working on a small project and got stuck.
Maybe somebody coud lead me?

I'm running a full node and want a py-script to check the balance of an address,
That's the code i am using, but really no idea, how the url needs to look, to get get the balance from my bitcore full node, already tried "http://localhost:3001/insight/"+address+"" but, didn't work!

That's the original code i was using:

Code:
def scan(address,pkey): 
req = urllib2.Request("http://btc.blockr.io/api/v1/address/info/"+address+"")
res = urllib2.urlopen(req)
json_data = json.load(res)
psycodad
Legendary
*
Offline Offline

Activity: 1656
Merit: 1830


精神分析的爸


View Profile
August 17, 2016, 07:13:36 PM
 #2

If you want to check against your local node, the below python module would probably be better suited for the job (though it probably can be done in some more complicated way with urllib2):

https://en.bitcoin.it/wiki/API_reference_%28JSON-RPC%29#Python

Also AFAIK to query the balance of any address and not just those of your wallet you'd need to run the node with -txindex, see below URL:

https://en.bitcoin.it/wiki/Running_Bitcoin

HTH

donGeilo (OP)
Full Member
***
Offline Offline

Activity: 169
Merit: 100



View Profile
August 17, 2016, 07:28:53 PM
 #3

First thx for your reply, but
how should the code lines look then?

It's a bitcored full node with insight api, so think -txindex is not suitable?

Adriandmen
Sr. Member
****
Offline Offline

Activity: 292
Merit: 251


Telegram: @Adriandmen


View Profile
August 17, 2016, 07:36:16 PM
 #4

The syntax seems wrong. I'm assuming this is Python 2.

I haven't tested it yet, but does the following work?:

Code:
req = urllib2.Request("http://btc.blockr.io/api/v1/address/info/"+address)
res = urllib2.urlopen(req)
json_data = json.loads(res.read())
psycodad
Legendary
*
Offline Offline

Activity: 1656
Merit: 1830


精神分析的爸


View Profile
August 17, 2016, 08:09:13 PM
 #5

I don't know about insight api, however what I said regarding txindex is wrong: that doesn't give you the ability query balances, just all transactions (from which you can extract all balances if you for example store them in a db like a block explorer does).

donGeilo (OP)
Full Member
***
Offline Offline

Activity: 169
Merit: 100



View Profile
August 17, 2016, 08:37:14 PM
 #6

Thx again, but nothing worked!

I think i'm doing somthing wrong with the Url and the json_data quering.
kaicrypzen
Hero Member
*****
Online Online

Activity: 1344
Merit: 656



View Profile
August 17, 2016, 09:16:07 PM
 #7

The original code seems okay to me, though there are somethings that bother me:
- The scan function has a parameter named pkey which it doesn't use ...
- I don't know why +"" is added.
- urlopen can directly take the (string) url as parameter, I don't see why call Request first ...

Normally, both json.load(res) and json.loads(res.read()) should work ...

What version of python are you using?

Do you get an exception when running your code?


donGeilo (OP)
Full Member
***
Offline Offline

Activity: 169
Merit: 100



View Profile
August 17, 2016, 09:31:28 PM
 #8

ATM i'm using python 2.7.12

i copied these line from github from a script. Tried to modify it, but till now nothing worked.

If i'm typing an addres in the browser, the node works fine.

Edit:
The code like it is, works, just when i try to point it to my localhost, nothing happens




kaicrypzen
Hero Member
*****
Online Online

Activity: 1344
Merit: 656



View Profile
August 17, 2016, 09:45:48 PM
 #9

The code like it is, works, just when i try to point it to my localhost, nothing happens

What does print res.read() return?

donGeilo (OP)
Full Member
***
Offline Offline

Activity: 169
Merit: 100



View Profile
August 17, 2016, 09:51:32 PM
 #10

Just an error: unexpected indent
donGeilo (OP)
Full Member
***
Offline Offline

Activity: 169
Merit: 100



View Profile
August 17, 2016, 09:58:31 PM
 #11

Maybe you got an easy clean solution, to check the balance of some addresses in a list, on a local node?

Cause the script from github has got much crap in it!
https://github.com/TheZ3ro/bitcoin-privkey-bruteforce/blob/master/btcscan.py
kaicrypzen
Hero Member
*****
Online Online

Activity: 1344
Merit: 656



View Profile
August 17, 2016, 10:04:34 PM
 #12

Just an error: unexpected indent

So there seem to be an issue with indentation, maybe a mix of tabs and spaces ...

Did you try running the lines directly in a python interpreter, like this:

Code:
import urllib2
import json
address = "your_address"
req = urllib2.Request("http://btc.blockr.io/api/v1/address/info/"+address)
res = urllib2.urlopen(req)
print res.read()
json_data = json.load(res)
print json_data

Adriandmen
Sr. Member
****
Offline Offline

Activity: 292
Merit: 251


Telegram: @Adriandmen


View Profile
August 17, 2016, 10:09:12 PM
 #13

Maybe you got an easy clean solution, to check the balance of some addresses in a list, on a local node?

Cause the script from github has got much crap in it!
https://github.com/TheZ3ro/bitcoin-privkey-bruteforce/blob/master/btcscan.py

This one also works:

Code:
import urllib2
import json

address = raw_input("Enter Address >> ")
urlRequest = urllib2.Request("https://blockchain.info/rawaddr/" + address)
data = urllib2.urlopen(urlRequest).read()

json_data = json.loads(data)
print "Final Balance: " + str(json_data['final_balance'])
donGeilo (OP)
Full Member
***
Offline Offline

Activity: 169
Merit: 100



View Profile
August 17, 2016, 10:14:57 PM
 #14

Yes, these works great!

But need it to read the addresses from a list.
kaicrypzen
Hero Member
*****
Online Online

Activity: 1344
Merit: 656



View Profile
August 17, 2016, 10:16:39 PM
 #15

Maybe you got an easy clean solution, to check the balance of some addresses in a list, on a local node?

If it is parsing the output of a url to get the balance then I think it can be done.

I just need some info:
- Where are the addresses? in a file? in the code in a list object?
- What is the call you perform for the local node i.e. the url which you use to access information about the BTC address? And do you expect the same kind of output that http://btc.blockr.io/api/v1/address/info/<address> gives?


donGeilo (OP)
Full Member
***
Offline Offline

Activity: 169
Merit: 100



View Profile
August 17, 2016, 10:22:06 PM
 #16

I want to put the addresses in a textfile.
The output is ok.
Url supposed to be: http://localhost:3001/insight/ (not working right now)
kaicrypzen
Hero Member
*****
Online Online

Activity: 1344
Merit: 656



View Profile
August 17, 2016, 10:24:29 PM
 #17

Yes, these works great!

But need it to read the addresses from a list.

Then you might loop through your list, something like that:

Code:
balances = []

for address in addresses:
    urlRequest = urllib2.Request("https://blockchain.info/rawaddr/" + address)
    data = urllib2.urlopen(urlRequest).read()
    json_data = json.loads(data)
    balances.append("Balance of " + address + " is " + str(json_data['final_balance']))

"\n".join(balances)

kaicrypzen
Hero Member
*****
Online Online

Activity: 1344
Merit: 656



View Profile
August 17, 2016, 10:29:58 PM
 #18

I want to put the addresses in a textfile.

Let's assume there is one address per line, then you might replace the previous loop with:

Code:
f = open("addresses.txt", "r")

for address in f:
....

And if you want to write the results somewhere then use:

Code:
f = open("balances.txt", "w")

f.write(....)

Finally you might have something like:

Code:
addresses = open("addresses.txt", "r")
balances = open("balances.txt", "w")

for address in addresses:
    urlRequest = urllib2.Request("http://localhost:3001/insight/" + address)
    data = urllib2.urlopen(urlRequest).read()
    json_data = json.loads(data)
    balances.write("Balance of " + address + " is " + str(json_data["data"]["balance"]) + "\n") #use the right keys

addresses.close()
balances.close()

donGeilo (OP)
Full Member
***
Offline Offline

Activity: 169
Merit: 100



View Profile
August 17, 2016, 10:37:51 PM
 #19

then i need the prefix address in each line right?
kaicrypzen
Hero Member
*****
Online Online

Activity: 1344
Merit: 656



View Profile
August 17, 2016, 10:43:28 PM
 #20

then i need the prefix address in each line right?

If your txt file is like:
Code:
first_addr
second_addr
.....
last_addr
the code will work.

The github code seems to loop through lines which are prefixed with Address: and Privkey: but if your file contains only addresses, one per line, then the code will work just fine (it loops through the lines and affects the line's value to the variable address). Is this case? Or will you also have private keys in your txt file?

Pages: [1] 2 3 »  All
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!