API reference¶
Auto-generated from the source docstrings.
Client¶
ZKDevice ¶
ZKDevice(host: str = '', port: int = 4370, password: int = 0, timeout: float = 10.0, transport: Transport | None = None)
High-level read-only client for a ZKTeco device.
ZKDevice is the primary entry point of the library. It wraps a
:class:~openzk.transport.Transport and a :class:~openzk.session.Session
to expose convenient methods for reading device metadata, users, attendance
records, operation logs, and biometric templates. All operations are
read-only; nothing on the device is mutated.
Use it as a context manager so that the connection, authentication, SDK handshake, and capability read happen on entry and are cleanly torn down on exit.
Example
with ZKDevice("192.168.1.201") as dev: ... info = dev.device_info() ... users = dev.users() ... for record in dev.attendance(): ... print(record)
Initialize a device client.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
host
|
str
|
Device IP address or hostname. |
''
|
port
|
int
|
Device TCP service port (ZKTeco default |
4370
|
password
|
int
|
Numeric comm key used for CMD_AUTH; |
0
|
timeout
|
float
|
Socket connect/read timeout in seconds. |
10.0
|
transport
|
Transport | None
|
Optional pre-built :class: |
None
|
Source code in src/openzk/client.py
__enter__ ¶
Open the session and read device capabilities.
Performs connect, authentication, the SDK handshake, and a capability
read (caching the result for :meth:device_info).
Returns:
| Name | Type | Description |
|---|---|---|
This |
ZKDevice
|
class: |
Source code in src/openzk/client.py
__exit__ ¶
Restore the device SDK handshake flag and disconnect.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*exc
|
object
|
Standard exception tuple from the |
()
|
option ¶
Read a raw device option by name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The option key (e.g. |
required |
Returns:
| Type | Description |
|---|---|
str | None
|
The raw option value as a string, or |
str | None
|
return a value for that option. |
Source code in src/openzk/client.py
device_info ¶
Read device metadata and record counts.
Combines cached capabilities (serial, platform, etc.) with a live read of the free-size table for user/finger/attendance/face counts.
Returns:
| Name | Type | Description |
|---|---|---|
A |
DeviceInfo
|
class: |
DeviceInfo
|
platform, the four record counts, and the full capabilities map. |
Source code in src/openzk/client.py
users ¶
Read all enrolled users from the device.
Returns:
| Type | Description |
|---|---|
list[User]
|
A list of :class: |
Source code in src/openzk/client.py
attendance ¶
Stream attendance (punch) records from the device.
Records are yielded lazily as they are parsed rather than materialized all at once, which keeps memory bounded on devices with large logs.
Yields:
| Type | Description |
|---|---|
Attendance
|
class: |
Example
with ZKDevice(host) as dev: ... for rec in dev.attendance(): ... print(rec.user_id, rec.timestamp)
Source code in src/openzk/client.py
op_logs ¶
Stream device operation-log entries.
Yielded lazily as they are parsed, like :meth:attendance.
Yields:
| Type | Description |
|---|---|
OpLog
|
class: |
Source code in src/openzk/client.py
user_photo ¶
Fetch a user's enrollment portrait as JPEG bytes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user_id
|
str
|
The device user ID (e.g. |
required |
Returns:
| Type | Description |
|---|---|
bytes | None
|
The JPEG bytes, or |
Raises:
| Type | Description |
|---|---|
ZKNotSupported
|
The device returned an unexpected response code. |
ZKProtocolError
|
The assembled photo size did not match the header. |
Example
with ZKDevice(host) as dev: ... jpeg = dev.user_photo("8")
Source code in src/openzk/client.py
face_templates ¶
Read enrolled face templates for every user.
Returns:
| Type | Description |
|---|---|
list[FaceTemplate]
|
A list of :class: |
list[FaceTemplate]
|
non-empty template are included. |
Raises:
| Type | Description |
|---|---|
ZKNotSupported
|
The device lacks face support (capability
|
Source code in src/openzk/client.py
fingerprint_templates ¶
Read all enrolled fingerprint templates.
Returns:
| Type | Description |
|---|---|
list[FingerTemplate]
|
A list of :class: |
Source code in src/openzk/client.py
Models¶
models ¶
Frozen dataclass models for the openzk client.
These are plain, immutable value objects returned by :class:openzk.client.ZKDevice.
They perform no I/O and hold only decoded device data.
DeviceInfo
dataclass
¶
DeviceInfo(ip: str, serial: str, firmware: str, platform: str, users_count: int, attendance_count: int, faces_count: int, fingers_count: int, capabilities: dict[str, str] = dict())
Device identity and record counts.
Attributes:
| Name | Type | Description |
|---|---|---|
ip |
str
|
The device IP/hostname the client connected to. |
serial |
str
|
Device serial number ( |
firmware |
str
|
Firmware version string ( |
platform |
str
|
Hardware/firmware platform string ( |
users_count |
int
|
Number of enrolled users. |
attendance_count |
int
|
Number of stored attendance records. |
faces_count |
int
|
Number of enrolled face templates. |
fingers_count |
int
|
Number of enrolled fingerprint templates. |
capabilities |
dict[str, str]
|
Raw capability option map read from the device. |
User
dataclass
¶
An enrolled device user.
Attributes:
| Name | Type | Description |
|---|---|---|
uid |
int
|
Internal numeric record index used by the device. |
user_id |
str
|
Human-facing user ID (badge/PIN), as a string. |
name |
str
|
Display name; falls back to |
privilege |
int
|
Privilege level code (e.g. user vs. admin). |
password |
str
|
User PIN/password, if set. |
group_id |
str
|
Access group identifier. |
card |
int
|
RFID card number ( |
Attendance
dataclass
¶
A single attendance (punch) record.
Attributes:
| Name | Type | Description |
|---|---|---|
user_id |
str
|
The user ID that produced the punch. |
timestamp |
datetime
|
When the punch occurred. |
status |
int
|
Verification status code (e.g. fingerprint, face, password). |
punch |
int
|
Punch state code (e.g. check-in/check-out). |
OpLog
dataclass
¶
A device operation-log entry.
Attributes:
| Name | Type | Description |
|---|---|---|
op |
int
|
Operation code identifying what occurred. |
timestamp |
datetime
|
When the operation occurred. |
target_uid |
int
|
The UID affected by the operation, when applicable. |
raw |
bytes
|
The original 16-byte record, preserved for further analysis. |
FingerTemplate
dataclass
¶
An enrolled fingerprint template.
Attributes:
| Name | Type | Description |
|---|---|---|
uid |
int
|
The user's internal numeric record index. |
finger_index |
int
|
Which finger this template represents (0-9). |
data |
bytes
|
The raw template bytes. |
FaceTemplate
dataclass
¶
An enrolled face template.
Attributes:
| Name | Type | Description |
|---|---|---|
uid |
int
|
The user's internal numeric record index. |
data |
bytes
|
The raw face-template bytes. |
UserPhoto
dataclass
¶
A user's enrollment portrait.
Attributes:
| Name | Type | Description |
|---|---|---|
user_id |
str
|
The user ID the photo belongs to. |
data |
bytes
|
The raw JPEG image bytes. |
Errors¶
errors ¶
Typed exceptions for openzk.
ZKError ¶
Bases: Exception
Base class for all openzk errors; catch this to handle any failure.
ZKUnreachable ¶
Bases: ZKError
Raised when the device socket cannot be reached (timeout/refused/reset).
ZKAuthFailed ¶
Bases: ZKError
Raised when the device rejects the connection or comm key during auth.
ZKProtocolError ¶
Bases: ZKError
Raised on malformed framing, bad magic, truncation, or a size mismatch.
ZKNotSupported ¶
Bases: ZKError
Raised when the device replies that a requested command is unsupported.
Initialize the error.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
function
|
str
|
Name of the requested device function/command. |
required |
response_code
|
int
|
The unexpected response code the device returned. |
required |
Source code in src/openzk/errors.py
Transport¶
transport ¶
Transport layer: abstract Transport, TCP implementation, command helpers.
Transport ¶
Bases: ABC
Abstract command transport for a ZK device.
Implementations move :class:~openzk.framing.Frame packets to and from a
device. The library depends only on this interface, which allows real
sockets and in-memory fakes (for tests) to be used interchangeably.
connect
abstractmethod
¶
disconnect
abstractmethod
¶
capture
abstractmethod
¶
Send a command and collect the response frames.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
command
|
int
|
The ZK command code to send. |
required |
data
|
bytes
|
Optional command payload. |
b''
|
Returns:
| Type | Description |
|---|---|
list[Frame]
|
The first response frame followed by any subsequent (streamed) |
list[Frame]
|
frames the device sends before going idle. |
Source code in src/openzk/transport.py
TcpTransport ¶
TcpTransport(host: str, port: int = 4370, timeout: float = 10.0, password: int = 0, idle: float = 1.5)
Bases: Transport
TCP implementation of :class:Transport for ZK devices.
Speaks the official ZK TCP framing: a connect/auth handshake on
:meth:connect, request/response with a rolling reply id, and idle-based
detection of the end of a streamed response in :meth:capture.
Initialize the TCP transport.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
host
|
str
|
Device IP address or hostname. |
required |
port
|
int
|
Device TCP service port (ZKTeco default |
4370
|
timeout
|
float
|
Socket connect/read timeout in seconds. |
10.0
|
password
|
int
|
Numeric comm key used for CMD_AUTH; |
0
|
idle
|
float
|
Seconds of silence used to detect the end of a streamed
multi-frame response in :meth: |
1.5
|
Source code in src/openzk/transport.py
connect ¶
Open the socket and perform connect + optional auth.
Sends CMD_CONNECT; if the device replies CMD_ACK_UNAUTH, follows up with CMD_AUTH using the scrambled comm key.
Raises:
| Type | Description |
|---|---|
ZKUnreachable
|
The socket connection could not be established. |
ZKAuthFailed
|
The device rejected the connection or comm key. |
Source code in src/openzk/transport.py
disconnect ¶
Send CMD_EXIT and close the socket.
Errors during the exit exchange are suppressed; the socket is always closed. Safe to call when already disconnected.
Source code in src/openzk/transport.py
capture ¶
Send a command and read until the device goes idle.
Sends one request, then keeps reading frames using the idle timeout
to detect the end of a streamed response.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
command
|
int
|
The ZK command code to send. |
required |
data
|
bytes
|
Optional command payload. |
b''
|
Returns:
| Type | Description |
|---|---|
list[Frame]
|
The first response frame followed by any streamed follow-on frames. |
Raises:
| Type | Description |
|---|---|
ZKProtocolError
|
A frame had bad magic or the socket closed mid-read. |
Source code in src/openzk/transport.py
first_data ¶
Concatenate the payloads of all frames matching a command.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
frames
|
list[Frame]
|
The frames to scan. |
required |
want_cmd
|
int
|
The command code to match. |
required |
Returns:
| Type | Description |
|---|---|
bytes
|
The concatenated |
bytes
|
|
Source code in src/openzk/transport.py
read_buffer ¶
Read a device table via the buffered SDK flow.
Issues CMD_PREPARE_BUFFER for (command, fct, ext), then pulls the result
in MAX_CHUNK-sized CMD_READ_BUFFER requests and frees the buffer. Mirrors
the official SDK / pyzk read_with_buffer flow.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
transport
|
Transport
|
The transport used to exchange frames. |
required |
command
|
int
|
The underlying data command (e.g. CMD_DB_RRQ). |
required |
fct
|
int
|
The table/function selector for the read. |
required |
ext
|
int
|
Optional extension parameter passed to PREPARE_BUFFER. |
0
|
Returns:
| Type | Description |
|---|---|
bytes
|
The assembled table blob, or empty bytes if the device reports no data. |
Source code in src/openzk/transport.py
Session¶
session ¶
Session: connect + AUTH + SDKBuild handshake + capability read, restored on close.
Session ¶
Manage the device session lifecycle and option access.
Wraps a :class:~openzk.transport.Transport to read/write device options
and to perform the SDK handshake required for many table reads. On
:meth:open the original SDKBuild value is saved and set to "1";
:meth:close restores it before disconnecting so the device is left in its
prior state.
Initialize the session.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
transport
|
Transport
|
The transport used to exchange command frames. |
required |
Source code in src/openzk/session.py
read_option ¶
Read a single device option via CMD_OPTIONS_RRQ.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The option key to query. |
required |
Returns:
| Type | Description |
|---|---|
str | None
|
The option value (text after |
str | None
|
|
Source code in src/openzk/session.py
write_option ¶
Write a device option and refresh device state.
Sends CMD_OPTIONS_WRQ with name=value followed by CMD_REFRESHDATA.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The option key to set. |
required |
value
|
str
|
The value to assign. |
required |
Source code in src/openzk/session.py
open ¶
Connect and perform the SDK handshake.
Connects the transport, saves the current SDKBuild option (defaulting
to "0" if absent), then sets SDKBuild to "1" so the device
serves buffered table reads.
Source code in src/openzk/session.py
close ¶
Restore the SDK handshake flag and disconnect.
If :meth:open ran, restores the original SDKBuild value (ignoring
write errors) before disconnecting the transport.
Source code in src/openzk/session.py
capabilities ¶
Read the standard set of device capability options.
Queries each well-known option name and collects the ones the device answers with a non-empty value.
Returns:
| Type | Description |
|---|---|
dict[str, str]
|
A mapping of option name to value for every present capability. |