Randomly Execute NFC Commands

  • Share this:

Code introduction


This function randomly selects an NFC command from a given list and executes it on the specified NFC device, then returns the response of the command.


Technology Stack : PyNFC

Code Type : Function

Code Difficulty : Intermediate


                
                    
import random

def random_nfc_command(device, command_list):
    """
    Executes a random NFC command on a given NFC device.

    Args:
        device (PyNFCDevice): The NFC device object.
        command_list (list): List of NFC commands to choose from.

    Returns:
        str: The result of the executed command.
    """
    if not command_list:
        return "No commands available."

    selected_command = random.choice(command_list)
    response = device.connect(selected_command['target'])
    if response:
        response = device.transceive(selected_command['data'])
    else:
        response = "Failed to connect."

    return response                
              
Tags: