Looking for a Python library to connect to Bitcoin P2P network

8 replies 702 views
alexsatNewbie
Posts: 280 · Reputation: 15
#1Feb 22, 2021, 11:34 AM
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?
13 Reply Quote Share
whale420Senior Member
Posts: 734 · Reputation: 853
#2Feb 22, 2021, 03:52 PM
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.
11 Reply Quote Share
yield2016Full Member
Posts: 34 · Reputation: 538
#3Feb 24, 2021, 02:10 AM
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.
6 Reply Quote Share
alexsatNewbie
Posts: 280 · Reputation: 15
#4Feb 25, 2021, 02:35 AM
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.
8 Reply Quote Share
gang2015Member
Posts: 693 · Reputation: 62
#5Feb 25, 2021, 05:05 AM
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.
9 Reply Quote Share
yield2016Full Member
Posts: 34 · Reputation: 538
#6Feb 25, 2021, 10:25 AM
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.
4 Reply Quote Share
alexsatNewbie
Posts: 280 · Reputation: 15
#7Feb 25, 2021, 04:30 PM
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!
11 Reply Quote Share
0xNodeMember
Posts: 680 · Reputation: 80
#8Feb 25, 2021, 09:11 PM
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.
7 Reply Quote Share
gang2015Member
Posts: 693 · Reputation: 62
#9Feb 25, 2021, 11:35 PM
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.
9 Reply Quote Share

Related topics