Click here to Skip to main content
15,868,141 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
Hi I am a beginner in python and I am working on the blockchain I want to store any hash that creates by any transaction. I want to transfer that hash to a local database. Can anyone tell me how can I do that? Here is my code. can anyone give me hint how to do it i will be veryhelpful

class Block:
    def __init__( self, previous_block_hash, transaction_list ):
        self.previous_block_hash = previous_block_hash
        self.transaction_list = transaction_list

        self.block_data = f"{' - '.join(transaction_list)} - {previous_block_hash}"
        self.block_hash = hashlib.sha256(self.block_data.encode()).hexdigest()


class Blockchain:
    def __init__( self ):
        self.chain = [ ]
        self.generate_genesis_block()

    def generate_genesis_block( self ):
        self.chain.append(Block("0", [ 'Genesis Block' ]))

    def create_block_from_transaction( self, transaction_list ):
        previous_block_hash = self.last_block.block_hash
        self.chain.append(Block(previous_block_hash, transaction_list))

    def display_chain( self ):
        for i in range(len(self.chain)):
            print(f"Hash {i + 1}: {self.chain [ i ].block_hash}\n")

    @property
    def last_block( self ):
        return self.chain [ -1 ]


**t1 = Time_sensitive_df
t2 = "abcdefghijklmnopqrstuvwxyz"
t3 = normal_df
myblockchain = Blockchain()
myblockchain.create_block_from_transaction(t1)
myblockchain.create_block_from_transaction(t2)
myblockchain.create_block_from_transaction(t3)
myblockchain.display_chain()**


What I have tried:

i try to do it with socket programming but get many errors.
but i am getting an error On client side . on line sock.sendall(bytes(data,"utf-8")) TypeError: encoding without a string argument Can any one please tell what ia m doing wrong.?

<pre>import socket
import pandas as pd
import pickle
import hashlib

df = pd.read_csv(r"C:\Users\DELL\OneDrive\Desktop\mythesisdataset.csv",
                 names=[ 'SBP', 'DBP', 'HEARTRATE', "Temperature" ])
normal_df = (df [ (df.SBP > 120) & (df.DBP > 90) & (df.HEARTRATE < 100) & (df [ 'Temperature' ] < 100) ])


class Block:
    def __init__( self, previous_block_hash, transaction_list ):
        self.previous_block_hash = previous_block_hash
        self.transaction_list = transaction_list

        self.block_data = f"{' - '.join(transaction_list)} - {previous_block_hash}"
        self.block_hash = hashlib.sha256(self.block_data.encode()).hexdigest()


class Blockchain:
    def __init__( self ):
        self.chain = [ ]
        self.generate_genesis_block()

    def generate_genesis_block( self ):
        self.chain.append(Block("0", [ 'Genesis Block' ]))

    def create_block_from_transaction( self, transaction_list ):
        previous_block_hash = self.last_block.block_hash
        self.chain.append(Block(previous_block_hash, transaction_list))

    def display_chain( self ):
        for i in range(len(self.chain)):
            print(f"Hash {i + 1}: {self.chain [ i ].block_hash}\n")

    @property
    def last_block( self ):
        return self.chain [ -1 ]


t1 = "abcdergfhiklm"
t2 = "abcdefghijklmnopqrstuvwxyz"
t3 = normal_df

myblockchain = Blockchain()
myblockchain.create_block_from_transaction(t1)
myblockchain.create_block_from_transaction(t2)
myblockchain.create_block_from_transaction(t3)
myblockchain.display_chain()

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = socket.gethostname()
port = 12345
s.bind((host, port))

print("host name:", host, " socket name:", socket)

s.listen()
while True:
    c, addr = s.accept()
    # print('Got connection from', addr, '...')
    df2 = pickle.dumps({myblockchain.create_block_from_transaction(t3)})
    c.send(df2)

    c.close()  # Close the connection


This is the client side
import socket
import pickle
import data as data
import sys
# Create a # socket (SOCK_STREAM means a TCP socket)

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

try:
    # Connect to server and send data
    sock.connect((socket.gethostname(), 12345))
    sock.sendall(bytes(data,"utf-8"))

    # Receive data from the server and shut down
    data = (sock.recv(1024))
    df3 = pickle.loads(data)
    print("Alert send")
    print("received data on fog node")
    print(df3)
finally:
    sock.close()
Posted
Updated 11-Jun-22 5:09am

1 solution

In your client code you have the following at line 12:
Python
sock.sendall(bytes(data,"utf-8"))

But you have not declared data, or created any information to send.
 
Share this answer
 
Comments
Ayesha Tassaduq 11-Jun-22 11:54am    
do you have any solution of it i try my best but its notworking
Ayesha Tassaduq 11-Jun-22 11:55am    
i want to transfer generated hash to a local server or on adatabse
Ayesha Tassaduq 11-Jun-22 12:21pm    
i remove that line now i am not sending any data .but still i am receiving none.
Richard MacCutchan 11-Jun-22 12:30pm    
You need to use the debugger to find out what is going on when you call the sock.recv function.
Ayesha Tassaduq 11-Jun-22 18:17pm    
i do debug but it shows nothing

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900