Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • python Blockchain code hashes transfer

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 5
    Answer it

    Hi, I am a beginner in python. I am working on blockchain and I am trying to transfer hashes through socket programming but on the client-side, I am receiving nothing. Can anyone please help me if this work can be done through a database like i can store hashes on a local database kindly guide me 

    Thank you.

    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()
    print(myblockchain.display_chain())
    
    
    while True:
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        host = socket.gethostname()
        port = 5007
        s.bind((host, port))
    
        print("host name:", host, " socket name:", socket)
    
        s.listen()
        c, addr = s.accept()
        # print('Got connection from', addr, '...')
        df2 = pickle.dumps(myblockchain.display_chain())
        c.send(df2)
    
        c.close()  # Close the connection
    
    
    Client side 
    
    import pickle
    import socket
    
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    
    try:
        # Connect to server and send data
        sock.connect((socket.gethostname(), 5007))
    
        # Receive data from the server and shut down
        data = (sock.recv(4096))
        #df3 = pickle.loads(data)
        print("Hash receive")
        print(data)
    finally:
        sock.close()
    

     

 0 Answer(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: