Don't miss our holiday offer - up to 50% OFF!
Ethereum: Starting point for listening to bitcoin network transactions in Python
Here is an article about creating a series of python commands that listens to web Bitcoin events and observe addresses stored in the MySQL database:
Ethereum: The starting point for listening to web Bitcoin events in Python
Bitcoin, the second largest cryptocurrency in market value, uses a decentralized node network to validate and save events. In this article, we analyze how to create a Python script that listens to the Bitcoin network and monitors addresses stored in the MySQL database.
Prerequisites
- You have basic information about Python and MySQL.
- You have a bitcoin node (for example, Bitcoin Core) installed on your machine.
- You have a mysql database created and filled with Bitcoin event information.
Step 1: Install the required libraries
Before we start, you need to install the following libraries:
Mysql-connector-python 'interact with mysql databases
- Py bitcoin-core
to work with bitcoin events (note: this is not a complete implementation of Bitcoin Knot)
To install these libraries with PIP:
Bash
PIP Install mysql-conector-python pybitcoincore
`
Step 2: Create Python Command Series
Create a new Python file, for example, “bitcoin_listener.py” and add the following code:
`Python
Bring mysql.connector
Bring Json
MySQL Database Settings
Db_host = ‘localhost’
Db_user = ‘your_username’
Db_password = ‘your_password’
Db_name = ‘bitcoin_transactions’
Bitcoin Node Settings
Bitcoin_node_url = ‘
Bitcoin_node_secret = ‘your_secret_key’
This should not be shared in public
DEF connect_to_tosusql ():
“” Create MySQL Database “” “
Return mysql.connector.connect (
host = db_host,
User = db_user,
password = db_password,
Database = db_Name
In
DEF get_bitcoin_transactions ():
“” “Recover Bitcoin’s Bitcoin events” “
Send the P2P request to the Bitcoin node
Answer = Request.get (bitcoin_node_url)
Data = json.loads (response.text)
Disassemble the addresses and quantities of the transaction
Events = []
From data in the data [‘events’]:
Address = item [‘de’]
Quantity = item [‘value’]
Events.append ({
‘Address’: Address,
‘Quantity’: Quantity
})
Return the events
DEF Main ():
“” Listen to Bitcoin web events “”
Create MySQL database connection
db_connection = connect_to_tusql ()
Search Node Bitcoin Events
Events = Get_BitCoin_Transactions ()
Ite transactions and follow the addresses in the MySQL database
at events at events:
Address = Event [‘Address’]
Quantity = event [‘quantity’]
Check that the address is in the MySQL database
cursor = db_connection.cursor ()
Query = “Select * Bitcoin_transations with address =%s and time stamp> =%s”
Course.
Result = Course.Fetchone ()
If the address is found, update your quantity and date and time registration for the database
If the result:
New_amount = event [‘quantity’] + 10
simulates the event with 10 units beyond
Questionnaire = “Update bitcoin_transations define quantity =%s, timestamp = now () where address =%s”
Course.Execute (Inquiry, (new_mount, address)))
Print the results on the console
Print (F “Address: {Address} | Number: {Quantity} | Timestamp: {datetime.now ()}”)
Close the MySQL database connection
db_connection.