pynfc

ctypeslib converted libnfc and libfreefare.

Usage

from pynfc import Nfc, Desfire, Timeout

n = Nfc("pn532_uart:/dev/ttyUSB0:115200")

DESFIRE_DEFAULT_KEY = b'\x00' * 8
MIFARE_BLANK_TOKEN = b'\xFF' * 1024 * 4

for target in n.poll():
    try:
        print(target.uid, target.auth(DESFIRE_DEFAULT_KEY if type(target) == Desfire else MIFARE_BLANK_TOKEN))
    except TimeoutException:
        pass

Develop

sudo apt install libfreefare-dev libclang-5.0-dev
git clone https://github.com/BarnabyShearer/pynfc.git
cd pynfc
python3 setup.py develop --user

Installation

sudo apt install libclang-dev libfreefare-dev
python3 -m pip install pynfc

pynfc

Libnfc and libfreefare with just enough of the interals to actually be usable.

class pynfc.Desfire(target: int)

Specialize for DesFire devices.

auth(key: bytes, keyno: int = 0) bool

Authenticate.

class pynfc.Mifare(target: int)

Specialize for Mifare devices.

auth(data: bytes, sector: int = 1, akey: bool = True) bool

Authenticate.

class pynfc.Nfc(device: Optional[str] = None, log_level: Optional[str] = None)

A slightly pythonic interface.

poll(modulations: Tuple[Tuple[int, int]] = ((1, 3),), times: int = 255, delay: int = 1) Iterator[Target]

Wait for device to enter field.

class pynfc.Target(target: int)

Target devices that may enter our field.

property type

Type of this device.

property uid

UID of the device.

(can vary in length based on type) (do not rely on them being absolutly unique)

class pynfc.Timeout(timeoutlength: Optional[int] = 1)

Watchdog to timeout functions.

exception pynfc.TimeoutException

Timeout Exception.

pynfc.classic_auth(*args: Any, **kwargs: Any) Any

Wrap function.

pynfc.classic_connect(*args: Any, **kwargs: Any) Any

Wrap function.

pynfc.desfire_auth(*args: Any, **kwargs: Any) Any

Wrap function.

pynfc.desfire_connect(*args: Any, **kwargs: Any) Any

Wrap function.

pynfc.poll(*args: Any, **kwargs: Any) Any

Wrap function.

pynfc.tag_new(*args: Any, **kwargs: Any) Any

Wrap function.

Indices and tables