Exemple #1
0
    
    # -----  START P2P COMUNICATION ----- #
    print("Second Response: CHOSEN MODE: {}\n IV: {}\n KEY: {}\n ROLE:{} ".format(chosenMode, n_iv, key, role))
    input("Start comunication, press a key and enter: ")

    if role == THE_READER: 
        listenerSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)  # prepare connection 
        listenerSocket.bind(('localhost',6000))                             #                
        listenerSocket.listen(1)                                            #    
        connection, addr = listenerSocket.accept()                          #                   

        nr_blocks = int(connection.recv(64).decode("utf-8"))                #read how many 8*16 size blocks are expected
        for i in range(nr_blocks+1):   
            big_blocks = connection.recv(8*16)                              #read a 8*16 block of message
            res = Response(big_blocks ,n_cipher, iv = n_iv, ctype = 'file') 
            print(i,"-->" ,res.body().strip(), end="\n\n", flush=True)

            req = Request({    #inform server you are done reading 8 blocks
                "code": 3
            }, e_cipher)
            res = send(req)


            req = Request({    #ask for permission to read again
                "code": 4
            }, e_cipher)
            res = send(req)
            while res.body()["code"] == "Not Ready":  # wait for permission to read again
                #time.sleep(1)
                res = send(req)
        print("Finished reading File.")