<p>Simple example, using Python 3 and the <a href="http://pypi.python.org/pypi/websocket-client/" rel="nofollow noreferrer">websocket-client</a> library.</p>

<pre><code>from websocket import create_connection

ws = create_connection("wss://ws.blockchain.info/inv")
ws.send("""{"op":"unconfirmed_sub"}""")
while True:
    tx = ws.recv()
    print("tx!")
    with open("bitcointx.json", "a") as f:
        f.write(tx + "\n####\n")
</code></pre>

CC BY-SA 3.0
2017-01-23T19:41:18.657Z
2017-01-23T19:41:18.657Z
Nick ODell
<p>I am doing a PhD in economics and I am currently working on bitcoin. I am trying to understand how senders decide how much fee they include in transactions. Since in a couple decades miners will be rewarded mostly with fees, I think this is an important question. It would be great if I could have data on all the unconfirmed transactions. This would enable me to see, for each transaction, how much fee it includes given the available information, that is how many transactions are already in the mempool and how much fee do those include. Of course I can see those transactions on blockchain.info, for instance, but is there a way I could save them on a file?</p>

<p>Thanks to a very clear video on you tube I can receive the transactions on my browser's console log using blockchain.info's websocket but how can I print, let's say a couple hundred thousands of them, in a textfile?</p>

<p>Could anyone help me? Thank you very much!</p>

<api><blockchain.info><websockets>
print websocket messages in a textfile
benjiw
# print websocket messages in a textfile

(Asked by: username_0 on 2017-01-23)

I am doing a PhD in economics and I am currently working on bitcoin. I am trying to understand how senders decide how much fee they include in transactions. Since in a couple decades miners will be rewarded mostly with fees, I think this is an important question. It would be great if I could have data on all the unconfirmed transactions. This would enable me to see, for each transaction, how much fee it includes given the available information, that is how many transactions are already in the mempool and how much fee do those include. Of course I can see those transactions on blockchain.info, for instance, but is there a way I could save them on a file?

Thanks to a very clear video on you tube I can receive the transactions on my browser's console log using blockchain.info's websocket but how can I print, let's say a couple hundred thousands of them, in a textfile?

Could anyone help me? Thank you very much!

**Tags:** api, blockchain.info, websockets


---

## 1 answers

### Answer by username_1 on 2017-01-23. Score: 2 | Accepted answer

Simple example, using Python 3 and the websocket-client library.
from websocket import create_connection

ws = create_connection("wss://ws.blockchain.info/inv")
ws.send("""{"op":"unconfirmed_sub"}""")
while True:
    tx = ws.recv()
    print("tx!")
    with open("bitcointx.json", "a") as f:
        f.write(tx + "\n####\n")


---

