Example #1
0
    def recv_file(self, client_sock):
        """ Create an ephemeral socket, receive files from the client, and 
        store in the current working directory.
        @param client_sock: client socket to send ephemeral port number.
        """
        # Create ephemeral socket
        ephem_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        ephem_sock.bind(('', 0))  # 0 = first available port
        ephem_sock.listen(1)

        # Send the ephemeral port number to client
        ephem_name = ephem_sock.getsockname()
        protocol.send_msg(client_sock, str(ephem_name[1]).encode())

        # Accept any incoming connections on the ephemeral socket
        conn, addr = ephem_sock.accept()

        # Receive the file and store in cwd.
        try:
            filename = protocol.recv_msg(conn).decode()
            print('Receiving {} from {}'.format(filename,
                                                client_sock.getsockname()[0]))
            filedata = protocol.recv_msg(conn).decode()

            # Check file integrity
            md5_recv = protocol.recv_msg(conn).decode()
            md5_local = hashlib.md5(filedata.encode()).hexdigest()
            if md5_recv != md5_local:
                print('Corrupt file data during transfer.')
                return
        except Exception as e:
            print(e)
            print('Error receiving file {}'.format(filename))
            print('Unsuccessful transfer.')
            return

        with open(filename, 'w') as outfile:  # write data file to file
            outfile.write(filedata)
        print('Transfer complete.')

        # Close the ephemeral socket.
        conn.close()
        ephem_sock.close()
Example #2
0
    def send_file(self, client_addr, ephem_port, filename):
        """ Create an ephemeral socket, send ephemeral socket port, and
        send the file data to the requesting client.
        @param client_addr: IP address of the client
        @param ephem_port: ephemeral socket port number
        @param filename: name of file to send
        """
        if self.is_valid_file(filename):
            data = open(filename, 'rb').read()
        else:
            data = 'NXFILE'.encode()
            ephem_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            ephem_sock.connect((client_addr, ephem_port))
            protocol.send_msg(ephem_sock, data)
            ephem_sock.close()
            return

        # Create ephemeral socket
        ephem_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        ephem_sock.connect((client_addr, ephem_port))

        # Send file data to client
        print('Sending {} to {}'.format(filename, client_addr))
        try:
            protocol.send_msg(ephem_sock, filename.encode())
            protocol.send_msg(ephem_sock, data)

            md5_send = hashlib.md5(data).hexdigest()
            protocol.send_msg(ephem_sock, md5_send.encode())  # send md5 hash
        except Exception as e:
            print('Error: {}'.format(e))
            print('Unsuccessful transfer of {}'.format(filename))
            ephem_sock.close()
            return
        print('Transfer complete.')
        ephem_sock.close()