I'm trying to find a Python script or library that can connect to the Bitcoin P2P network. I want to query blocks directly from random full nodes, like how an actual Bitcoin node would do it. Anyone know if there's a solution out there?
Looking for a Python library to connect to Bitcoin P2P network
8 replies 449 views
You might wanna check out the full node Bitcoind in Python. There's a guide that includes all the commands you need. Here are a couple of links to open-source resources that could help you out.
Not sure what you mean by random full nodes. Aren't they secure? If they're open, people could steal private keys. You probably need to connect to the RPC calls on a Bitcoin node. It’s a safer way to access commands from Python, and libraries like python-bitcoinlib can help with that.
I think you guys misunderstood. I don't want to connect to a local node using JSON-RPC, I already have that covered. What I need is a way to connect to the P2P network directly. The messaging protocol for that isn't the same as JSON-RPC, and I don’t think it exposes private keys.
If I got your needs right, BitcoinJ might fit the bill. It has SPV functionality and supports older BIP commands. It’s Java-based but claims you can run it through Python with Jython.
Got it now, you’re after a direct P2P connection and not just a JSON-RPC interface. python-bitcoinlib should still be useful. Also, consider creating your own custom implementation. bitcoinnet is another tool that’s focused on P2P connections.
Thanks for the tips! I just realized I was using an older version of python-bitcoinlib that didn’t have the P2P features. The latest version seems to be evolving, even if it’s not fully documented yet. Better updates might be on the way!
Those old libraries like python-bitcoin-utils and python-bitcoinlib do handle some P2P messages but they’re pretty much abandoned now. Plus, I’m not sure about BitcoinJ’s maintenance status either, but most commands should still work.
Not exactly what the OP asked, but NBitcoin has SPV features too. It's less popular than BitcoinJ and written in C#. If BitcoinJ isn't active anymore, I wouldn’t bother mentioning it.
Related topics
- Sparrow Can't Connect to Bitcoin Core When Launched via systemd 2
- New Bitcoin Improvement Proposal with $100 Reward 9
- Running Bitcoin Core on a Laptop with Limited Storage 20
- Creating Custom Bitcoin Addresses with Vanitygen 20
- Slow Sync Issues with Bitcoin Core 4
- Contest to Crack Bitcoin's Security and Win BTC 21