.\" -*- mode: troff; coding: utf-8 -*- .\" Automatically generated by Podwrapper::Man 1.24.0 (Pod::Simple 3.45) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>. .ie n \{\ . ds C` "" . ds C' "" 'br\} .el\{\ . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" .\" Required to disable full justification in groff 1.23.0. .if n .ds AD l .\" ======================================================================== .\" .IX Title "libnbd-python 3" .TH libnbd-python 3 2025-12-16 libnbd-1.24.0 LIBNBD .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH NAME libnbd\-python \- how to use libnbd from Python .SH SYNOPSIS .IX Header "SYNOPSIS" .Vb 5 \& #!/usr/bin/python3 \& import nbd \& h = nbd.NBD() \& h.connect_uri("nbd://localhost") \& sector = h.pread(512, 0) .Ve .SH DESCRIPTION .IX Header "DESCRIPTION" This manual page documents how to use libnbd to access Network Block Device (NBD) servers from the Python programming language. The Python bindings work very similarly to the C bindings so you should start by reading \fBlibnbd\fR\|(3). .PP There is a convenient interactive command line shell called \&\fBnbdsh\fR\|(1) which makes it easier to play with the Python API. .PP Detailed documentation of the Python API is provided in the Python code. To read it, open \fBnbdsh\fR\|(1) and type: .PP .Vb 1 \& help(nbd) .Ve .PP There are some example Python scripts in the libnbd source repository under \fIpython/examples\fR or see https://gitlab.com/nbdkit/libnbd/tree/master/python/examples. .SH HANDLES .IX Header "HANDLES" Create a libnbd handle by calling \f(CW\*(C`nbd.NBD()\*(C'\fR. .PP You can either close the handle explicitly with \f(CW\*(C`h.close()\*(C'\fR (added in libnbd\ 1.16), or it will be closed automatically when it goes out of scope. .PP Calling any method on a handle that has been closed will raise \&\f(CW\*(C`nbd.ClosedHandle\*(C'\fR. .SH ERRORS .IX Header "ERRORS" Errors from API calls are turned into the Python \f(CW\*(C`nbd.Error\*(C'\fR exception (derived from \f(CW\*(C`Exception\*(C'\fR). The \f(CW\*(C`nbd.Error\*(C'\fR object has three properties of interest: .ie n .IP """ex.string""" 4 .el .IP \f(CWex.string\fR 4 .IX Item "ex.string" The full error message as a printable string. .Sp Example: \f(CW"nbd_pread: invalid state: START: the handle must be connected with the server: Transport endpoint is not connected"\fR .ie n .IP """ex.errnum""" 4 .el .IP \f(CWex.errnum\fR 4 .IX Item "ex.errnum" The errno object (from the Python \f(CW\*(C`errno\*(C'\fR module), \fIif\fR this is available. Not all errors that are raised will have an errno. .Sp Example: \f(CW\*(C`errno.EINVAL\*(C'\fR .Sp For common error codes see "Errno" in \fBlibnbd\fR\|(3). .ie n .IP """ex.errno""" 4 .el .IP \f(CWex.errno\fR 4 .IX Item "ex.errno" The errno as a string (or \f(CW\*(C`None\*(C'\fR if this is not available). .Sp Example: \f(CW"ENOTCONN"\fR .SH BUFFERS .IX Header "BUFFERS" Synchronous calls (eg. \f(CW\*(C`h.pwrite\*(C'\fR, \f(CW\*(C`h.pread\*(C'\fR) take and return normal Python \f(CW\*(C`bytearray\*(C'\fRs. .PP Asynchronous calls (eg. \f(CW\*(C`h.aio_pwrite\*(C'\fR, \f(CW\*(C`h.aio_pread\*(C'\fR) use the \&\f(CW\*(C`nbd.Buffer\*(C'\fR type, which is a persistent, modifiable, zero\-copy buffer. The buffer also supports indexing and slicing. These are more complicated to use, and in particular the caller must ensure the buffer remains valid while the asynchronous operation is taking place. .SH "SEE ALSO" .IX Header "SEE ALSO" \&\fBlibnbd\fR\|(3), \&\fBnbdsh\fR\|(1). .SH AUTHORS .IX Header "AUTHORS" Richard W.M. Jones .SH COPYRIGHT .IX Header "COPYRIGHT" Copyright Red Hat .SH LICENSE .IX Header "LICENSE" This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. .PP This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. .PP You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110\-1301 USA