from ykman.device import connect_to_device
from yubikit.core.smartcard import SmartCardConnection
from yubikit.piv import PivSession
# Connect to a YubiKey over a SmartCardConnection, which is needed for PIV.
connection, device, info = connect_to_device(
serial=123456, # Serial number of the YubiKey to connect to, can be omitted
connection_types=[SmartCardConnection], # Possible Connection types to allow
)
with connection: # This closes the connection after the block
piv = PivSession(connection)
attempts = piv.get_pin_attempts()
print(f"You have {attempts} PIN attempts left.")