xdrlib
— Encode and decode XDR data对XDR数据进行编码和解码¶
Source code: Lib/xdrlib.py
Deprecated since version 3.11: 自3.11版起已弃用:The xdrlib
module is deprecated (see PEP 594 for details).xdrlib
模块已弃用(有关详细信息,请参阅PEP 594)。
The xdrlib
module supports the External Data Representation Standard as described in RFC 1014, written by Sun Microsystems, Inc. June 1987. xdrlib
模块支持由Sun Microsystems,Inc.于1987年6月编写的RFC 1014中所述的外部数据表示标准。It supports most of the data types described in the RFC.它支持RFC中描述的大多数数据类型。
The xdrlib
module defines two classes, one for packing variables into XDR representation, and another for unpacking from XDR representation. xdrlib
模块定义了两个类,一个用于将变量打包到XDR表示中,另一个用于从XDR表示拆包。There are also two exception classes.还有两个异常类。
-
class
xdrlib.
Packer
¶ Packer
is the class for packing data into XDR representation.是将数据打包为XDR表示的类。ThePacker
class is instantiated with no arguments.Packer
类实例化时没有参数。
-
class
xdrlib.
Unpacker
(data)¶ Unpacker
is the complementary class which unpacks XDR data values from a string buffer.是从字符串缓冲区解包XDR数据值的补充类。The input buffer is given as data.输入缓冲区作为data给出。
See also
- RFC 1014 - XDR:
External Data Representation Standard外部数据表示标准 This RFC defined the encoding of data which was XDR at the time this module was originally written.该RFC定义了在最初编写该模块时为XDR的数据编码。It has apparently been obsoleted by RFC 1832.它显然已经被RFC 1832废弃。- RFC 1832 -
XDR: External Data Representation StandardXDR:外部数据表示标准 Newer RFC that provides a revised definition of XDR.更新的RFC提供了XDR的修订定义。
Packer Objects打包机对象¶
Packer
instances have the following methods:实例具有以下方法:
-
Packer.
get_buffer
()¶ Returns the current pack buffer as a string.以字符串形式返回当前包缓冲区。
-
Packer.
reset
()¶ Resets the pack buffer to the empty string.将压缩缓冲区重置为空字符串。
In general, you can pack any of the most common XDR data types by calling the appropriate 通常,通过调用适当的pack_type()
method. pack_type()
方法,可以打包任何最常见的XDR数据类型。Each method takes a single argument, the value to pack. 每个方法都有一个参数,即要打包的值。The following simple data type packing methods are supported: 支持以下简单的数据类型打包方法:pack_uint()
, pack_int()
, pack_enum()
, pack_bool()
, pack_uhyper()
, and pack_hyper()
.pack_uint()
、pack_int()
、pack_enum()
、pack_bool()
、pack_uhyper()
和pack_hyper()
。
-
Packer.
pack_float
(value)¶ Packs the single-precision floating point number value.压缩单精度浮点数字value。
-
Packer.
pack_double
(value)¶ Packs the double-precision floating point number value.压缩双精度浮点数字value。
The following methods support packing strings, bytes, and opaque data:以下方法支持打包字符串、字节和不透明数据:
-
Packer.
pack_fstring
(n, s)¶ Packs a fixed length string, s.打包固定长度的字符串s。n is the length of the string but it is not packed into the data buffer.n是字符串的长度,但它未打包到数据缓冲区中。The string is padded with null bytes if necessary to guaranteed 4 byte alignment.如果需要保证4字节对齐,则用空字节填充字符串。
-
Packer.
pack_fopaque
(n, data)¶ Packs a fixed length opaque data stream, similarly to打包固定长度的不透明数据流,类似于pack_fstring()
.pack_fstring()
。
-
Packer.
pack_string
(s)¶ Packs a variable length string, s.打包可变长度字符串s。The length of the string is first packed as an unsigned integer, then the string data is packed with首先将字符串的长度打包为无符号整数,然后使用pack_fstring()
.pack_fstring()
打包字符串数据。
-
Packer.
pack_opaque
(data)¶ Packs a variable length opaque data string, similarly to打包可变长度的不透明数据字符串,类似于pack_string()
.pack_string()
。
-
Packer.
pack_bytes
(bytes)¶ Packs a variable length byte stream, similarly to打包可变长度字节流,类似于pack_string()
.pack_string()
。
The following methods support packing arrays and lists:以下方法支持打包数组和列表:
-
Packer.
pack_list
(list, pack_item)¶ Packs a list of homogeneous items.打包同质项目list。This method is useful for lists with an indeterminate size; i.e. the size is not available until the entire list has been walked.此方法适用于大小不确定的列表;也就是说,在浏览完整个列表后,才可以使用大小。For each item in the list, an unsigned integer对于列表中的每个项,首先压缩一个无符号整数1
is packed first, followed by the data value from the list.1
,然后是列表中的数据值。pack_itemis the function that is called to pack the individual item.是为打包单个项目而调用的函数。At the end of the list, an unsigned integer在列表的末尾,压缩了一个无符号整数0
is packed.0
。For example, to pack a list of integers, the code might appear like this:例如,要打包整数列表,代码可能如下所示:import xdrlib
p = xdrlib.Packer()
p.pack_list([1, 2, 3], p.pack_int)
-
Packer.
pack_farray
(n, array, pack_item)¶ Packs a fixed length list (array) of homogeneous items.打包同质项的固定长度列表(array)。n is the length of the list; it is not packed into the buffer, but an是列表的长度;它不会被打包到缓冲区中,但是如果ValueError
exception is raised iflen(array)
is not equal to n.len(array)
不等于n,就会引发ValueError
异常。As above, pack_item is the function used to pack each element.如上所述,pack_item是用于打包每个元素的函数。
-
Packer.
pack_array
(list, pack_item)¶ Packs a variable length list of homogeneous items.打包同质项目的可变长度列表。First, the length of the list is packed as an unsigned integer, then each element is packed as in首先,将列表的长度打包为无符号整数,然后将每个元素打包为上面的pack_farray()
above.pack_farray()
。
Unpacker Objects解包器对象¶
The Unpacker
class offers the following methods:Unpacker
类提供以下方法:
-
Unpacker.
reset
(data)¶ Resets the string buffer with the given data.使用给定data重置字符串缓冲区。
-
Unpacker.
get_position
()¶ Returns the current unpack position in the data buffer.返回数据缓冲区中的当前解包位置。
-
Unpacker.
set_position
(position)¶ Sets the data buffer unpack position to position.将数据缓冲区解包位置设置为position。You should be careful about using在使用get_position()
andset_position()
.get_position()
和set_position()
。
-
Unpacker.
get_buffer
()¶ Returns the current unpack data buffer as a string.以字符串形式返回当前解包数据缓冲区。
-
Unpacker.
done
()¶ Indicates unpack completion.表示解包完成。Raises an如果尚未解包所有数据,则引发Error
exception if all of the data has not been unpacked.Error
异常。
In addition, every data type that can be packed with a 此外,可以用Packer
, can be unpacked with an Unpacker
. Packer
打包的每种数据类型都可以用Unpacker
解包。Unpacking methods are of the form 解包方法的形式为unpack_type()
, and take no arguments. unpack_type()
,不接受任何参数。They return the unpacked object.它们返回未打包的对象。
-
Unpacker.
unpack_float
()¶ Unpacks a single-precision floating point number.解压缩单精度浮点数字。
-
Unpacker.
unpack_double
()¶ Unpacks a double-precision floating point number, similarly to解压缩双精度浮点数,类似于unpack_float()
.unpack_float()
。
In addition, the following methods unpack strings, bytes, and opaque data:此外,以下方法解包字符串、字节和不透明数据:
-
Unpacker.
unpack_fstring
(n)¶ Unpacks and returns a fixed length string.解压缩并返回固定长度的字符串。n is the number of characters expected.n是预期的字符数。Padding with null bytes to guaranteed 4 byte alignment is assumed.假设使用空字节填充以保证4字节对齐。
-
Unpacker.
unpack_fopaque
(n)¶ Unpacks and returns a fixed length opaque data stream, similarly to解压缩并返回固定长度的不透明数据流,类似于unpack_fstring()
.unpack_fstring()
。
-
Unpacker.
unpack_string
()¶ Unpacks and returns a variable length string.解压缩并返回可变长度字符串。The length of the string is first unpacked as an unsigned integer, then the string data is unpacked with首先将字符串长度解包为无符号整数,然后使用unpack_fstring()
.unpack_fstring()
解包字符串数据。
-
Unpacker.
unpack_opaque
()¶ Unpacks and returns a variable length opaque data string, similarly to解压缩并返回一个可变长度的不透明数据字符串,类似于unpack_string()
.unpack_string()
。
-
Unpacker.
unpack_bytes
()¶ Unpacks and returns a variable length byte stream, similarly to解压缩并返回可变长度字节流,类似于unpack_string()
.unpack_string()
。
The following methods support unpacking arrays and lists:以下方法支持拆包数组和列表:
-
Unpacker.
unpack_list
(unpack_item)¶ Unpacks and returns a list of homogeneous items.打开包装并返回同质项目列表。The list is unpacked one element at a time by first unpacking an unsigned integer flag.通过首先打开一个无符号整数标志,一次打开一个元素。If the flag is如果标志为1,则该项将被解压缩并附加到列表中。1
, then the item is unpacked and appended to the list.A flag of0
indicates the end of the list.0
标志表示列表的结束。unpack_itemis the function that is called to unpack the items.是为解包项而调用的函数。
-
Unpacker.
unpack_farray
(n, unpack_item)¶ Unpacks and returns (as a list) a fixed length array of homogeneous items.打开并返回(作为列表)一个固定长度的同类项数组。n is number of list elements to expect in the buffer.n是缓冲区中预期的列表元素数。As above, unpack_item is the function used to unpack each element.如上所述,unpack_item是用于解压缩每个元素的函数。
-
Unpacker.
unpack_array
(unpack_item)¶ Unpacks and returns a variable length list of homogeneous items.解压缩并返回一个长度可变的同类项目list。First, the length of the list is unpacked as an unsigned integer, then each element is unpacked as in首先,将列表的长度解包为无符号整数,然后将每个元素解包为上面的unpack_farray()
above.unpack_farray()
。
Exceptions例外情况¶
Exceptions in this module are coded as class instances:此模块中的异常被编码为类实例:
-
exception
xdrlib.
Error
¶ The base exception class.基本异常类。Error
has a single public attributemsg
containing the description of the error.Error
有一个包含错误描述的公共属性msg
。
-
exception
xdrlib.
ConversionError
¶ Class derived from从Error
.Error
派生的类。Contains no additional instance variables.不包含其他实例变量。
Here is an example of how you would catch one of these exceptions:下面是一个如何捕获其中一个异常的示例:
import xdrlib
p = xdrlib.Packer()
try:
p.pack_double(8.01)
except xdrlib.ConversionError as instance:
print('packing the double failed:', instance.msg)