<p>In the end I went with <a href="https://blockchain.info/api/api_websocket" rel="nofollow">The websocket API from Blockchain</a> in combination with the python websocket client from <a href="https://github.com/liris/websocket-client" rel="nofollow">Hiroki Ohtani</a>. This mechanism is really instantaneous and seems stable and usable. However, I still have concerns around having to put my eggs in one basket. So, I double check and also use <a href="https://www.blocktrail.com/api/docs#api_webhooks" rel="nofollow">Blocktrail's webhooks api</a>.</p>

<p>This system still does not give me 100% foolproof security but I can live with this setup, until a better system comes along, or I can make something better.</p>

<p>Thank you all for helping out, and I hope this answer may serve as an idea/basis for your own Bitcoin projects!</p>

CC BY-SA 3.0
2015-10-26T10:24:39.910Z
2015-10-26T10:24:39.910Z
DDecoene
<p>If you just listen to transactions on the network without having a validating node of your own you will have absolutely no way of knowing if they are legitimate or not. You must have a fully validating node locally, otherwise transactions can be cost-lessly fabricated to spend any amount of money anywhere. There is no recourse if you get this wrong, this is primarily why nobody has implemented any sort of validation in Python, <a href="https://github.com/sebicas/bitcoin-sniffer" rel="nofollow">there's various bits of the p2p protocol on Github</a>, but it does exactly <em>zero consensus validation on the data it receives</em>. </p>

<p>The current <code>master</code> of Bitcoin Core (and the up coming 0.12 release at the end of the year) both contain a ZMQ interface which gives you a real time supply of validated transactions. A full node (pruned) can be run in as little as 2GB of space, and isn't very resource intensive for somebody running a business. There's example code for that available on github as <a href="https://github.com/bitcoin/bitcoin/blob/master/contrib/zmq/zmq_sub.py" rel="nofollow">zmq_sub.py</a> which should get you started, just be aware that ZMQ has no guarantee of deliverability, some messages may be silently dropped if there are connectivity issues. </p>

<p>It's important to keep in mind that an unconfirmed transaction has no guarantee of being confirmed, and may be invalidated at any point in time. Accepting zero confirmations is extremely risky, though updating a user interface with a list of unconfirmed transactions is perfectly reasonable behaviour. </p>

CC BY-SA 3.0
2015-10-22T14:54:18.237Z
I haven't looked at the problem this way. It is very true what you are saying. I need validated transactions and I need more than zero confirmations, yes. BUT, what I'm trying to achieve is that a customer can pay me instore at our POS. I can't tell my customer hey wait a couple of minutes 'till I get  x confirmations. He's used to instant verification of his debit/creditcard. I think I will need to implement levels of amount versus confirmations, like under $10 value = 0 conf, $100=1, ..., and $10.000+ = 6 conf.
DDecoene
CC BY-SA 3.0
2015-10-22T15:11:13.207Z
You can't safely do this with Bitcoin, it is not designed for it. Pretending otherwise can leave you an unlimited amount of money out of pocket, companies have lost hundreds of thousands of dollars to very unsophisticated attacks against zero confirmation transactions. Possibly with payment channels in the future instant transactions with Bitcoin might be an option, but not today.
Claris
CC BY-SA 3.0
2015-10-22T19:14:34.253Z
OT Danger: It is my desire to help make crypto's mainstream. I'm a founder of Ieper bitcoincity www.ieperbitcoinstad.be and involved in a similar project in Kortrijk, both in Belgium. To do this, we cannot use an altcoin, that could address these problems. All people know right now is Bitcoin. They have heard about it somewhere. We inform people with our initiatives. To get the ball rolling I wanted to offer storekeepers a platform for accepting BTC and get EUR deposited on their account, like bitpay and bitkassa, just local, Belgian, trusted. Aim is to help transition. How to they do it then?
DDecoene
CC BY-SA 3.0
2015-10-22T19:20:50.650Z
The longer term goal of my platform is that storekeepers will say 'hey, I don't need to convert to Eur no more, I can pay my suppliers with BTC'. This could then create an ecosystem that is sustainable. I compare BTC to internet in '93. People wondered what that http or @ meant and such. Now we are the pioneers in the financial revolution. Revolutions are risky ;-) I've never shied away from (calculated) risks. I'm not planning on going bankrupt but completely safe is an illusion in business anyhow. Thank you for your welcome input sir I really appreciate it.
DDecoene
CC BY-SA 3.0
2015-10-23T03:22:17.757Z
Nobility doesn't solve technical issues though
Claris
CC BY-SA 3.0
2015-10-22T12:34:41.613Z
2015-10-22T14:47:42.763Z
2015-10-22T14:47:42.763Z
Claris
<p>If you have a raw copy of the transaction, you could re-publish it over and over again until it gets confirmed. Even with very low transaction fees it should get confirmed in the end (but it might take long)</p>

CC BY-SA 3.0
2016-02-08T01:16:12.507Z
But how do you find out whether it's confirmed? And, on a related note, if it isn't confirmed, how do you know whether it's valid?
Nick ODell
CC BY-SA 3.0
2016-02-06T06:38:42.627Z
2016-02-06T06:38:42.627Z
Thomas
<p>I would like to create a python script that listens to the bitcoin network and monitors some addresses, stored in a mysql db. As soon as a transaction is broadcast with one of these addresses as an output, the script should run a function.</p>

<p>I do not need to store the whole blockchain, nor do I want to. The private keys are also not on the same machine, for security. There is no wallet on this machine, just a list of addresses in a DB.</p>

<p>For the moment I have implemented this with the webhooks from blockchain.info and also tested with blocktrail. It works but it does not work fast enough te achieve my goals, these solutions are just too slow to be practical. I need to be notified instantly.</p>

<p>I've looked at several options but none seem to fit as a good starting point. (BitcoinJ seems the closest but it is in Java) I cannot believe there is no such thing in existence, I just couldn't find one.</p>

<p>So, can you steer me in the right direction? </p>

<transactions><network><python>
Starting point for listening to bitcoin network transactions in Python
DDecoene
# Starting point for listening to bitcoin network transactions in Python

(Asked by: username_0 on 2015-10-22)

I would like to create a python script that listens to the bitcoin network and monitors some addresses, stored in a mysql db. As soon as a transaction is broadcast with one of these addresses as an output, the script should run a function.

I do not need to store the whole blockchain, nor do I want to. The private keys are also not on the same machine, for security. There is no wallet on this machine, just a list of addresses in a DB.

For the moment I have implemented this with the webhooks from blockchain.info and also tested with blocktrail. It works but it does not work fast enough te achieve my goals, these solutions are just too slow to be practical. I need to be notified instantly.

I've looked at several options but none seem to fit as a good starting point. (BitcoinJ seems the closest but it is in Java) I cannot believe there is no such thing in existence, I just couldn't find one.

So, can you steer me in the right direction?

**Tags:** transactions, network, python


---

## 3 answers

### Answer by username_0 on 2015-10-26. Score: 2

In the end I went with [The websocket API from Blockchain](https://blockchain.info/api/api_websocket) in combination with the python websocket client from [Hiroki Ohtani](https://github.com/liris/websocket-client). This mechanism is really instantaneous and seems stable and usable. However, I still have concerns around having to put my eggs in one basket. So, I double check and also use [Blocktrail's webhooks api](https://www.blocktrail.com/api/docs#api_webhooks).

This system still does not give me 100% foolproof security but I can live with this setup, until a better system comes along, or I can make something better.

Thank you all for helping out, and I hope this answer may serve as an idea/basis for your own Bitcoin projects!


---

### Answer by username_1 on 2015-10-22. Score: 2

If you just listen to transactions on the network without having a validating node of your own you will have absolutely no way of knowing if they are legitimate or not. You must have a fully validating node locally, otherwise transactions can be cost-lessly fabricated to spend any amount of money anywhere. There is no recourse if you get this wrong, this is primarily why nobody has implemented any sort of validation in Python, [there's various bits of the p2p protocol on Github](https://github.com/sebicas/bitcoin-sniffer), but it does exactly *zero consensus validation on the data it receives*.

The current `master` of Bitcoin Core (and the up coming 0.12 release at the end of the year) both contain a ZMQ interface which gives you a real time supply of validated transactions. A full node (pruned) can be run in as little as 2GB of space, and isn't very resource intensive for somebody running a business. There's example code for that available on github as [zmq\_sub.py](https://github.com/bitcoin/bitcoin/blob/master/contrib/zmq/zmq_sub.py) which should get you started, just be aware that ZMQ has no guarantee of deliverability, some messages may be silently dropped if there are connectivity issues.

It's important to keep in mind that an unconfirmed transaction has no guarantee of being confirmed, and may be invalidated at any point in time. Accepting zero confirmations is extremely risky, though updating a user interface with a list of unconfirmed transactions is perfectly reasonable behaviour.

> **Comments:**
> **username_0**: I haven't looked at the problem this way. It is very true what you are saying. I need validated transactions and I need more than zero confirmations, yes. BUT, what I'm trying to achieve is that a customer can pay me instore at our POS. I can't tell my customer hey wait a couple of minutes 'till I get  x confirmations. He's used to instant verification of his debit/creditcard. I think I will need to implement levels of amount versus confirmations, like under $10 value = 0 conf, $100=1, ..., and $10.000+ = 6 conf.  
> **username_1**: You can't safely do this with Bitcoin, it is not designed for it. Pretending otherwise can leave you an unlimited amount of money out of pocket, companies have lost hundreds of thousands of dollars to very unsophisticated attacks against zero confirmation transactions. Possibly with payment channels in the future instant transactions with Bitcoin might be an option, but not today.  
> **username_0**: OT Danger: It is my desire to help make crypto's mainstream. I'm a founder of Ieper bitcoincity www.ieperbitcoinstad.be and involved in a similar project in Kortrijk, both in Belgium. To do this, we cannot use an altcoin, that could address these problems. All people know right now is Bitcoin. They have heard about it somewhere. We inform people with our initiatives. To get the ball rolling I wanted to offer storekeepers a platform for accepting BTC and get EUR deposited on their account, like bitpay and bitkassa, just local, Belgian, trusted. Aim is to help transition. How to they do it then?  
> **username_0**: The longer term goal of my platform is that storekeepers will say 'hey, I don't need to convert to Eur no more, I can pay my suppliers with BTC'. This could then create an ecosystem that is sustainable. I compare BTC to internet in '93. People wondered what that http or @ meant and such. Now we are the pioneers in the financial revolution. Revolutions are risky ;-) I've never shied away from (calculated) risks. I'm not planning on going bankrupt but completely safe is an illusion in business anyhow. Thank you for your welcome input sir I really appreciate it.  
> **username_1**: Nobility doesn't solve technical issues though  

---

### Answer by username_2 on 2016-02-06. Score: 0

If you have a raw copy of the transaction, you could re-publish it over and over again until it gets confirmed. Even with very low transaction fees it should get confirmed in the end (but it might take long)

> **Comments:**
> **username_3**: But how do you find out whether it's confirmed? And, on a related note, if it isn't confirmed, how do you know whether it's valid?  

---

