📄 192.html
字号:
<p>
<A NAMe="idx1073747398"></a>
<a nAME="idx1073747399"></A>
<p>
<tt class="monofont">reset(data)</tt>棤
Resets the string buffer with the given data.
</p>
</p>
<p>
<a name="idx1073747400"></a>
<a NamE="idx1073747401"></a>
<p>
<tT clasS="monofont">get_position()</tt>棤
Returns the current unpack position in the data
buffer.
</p>
</P>
<P>
<A Name="idx1073747402"></a>
<A NAMe="idx1073747403"></a>
<p>
<tT CLAss="monofont">set_position(position)</tt>棤
Sets the data buffer unpack position to position. You should
be careful about using <TT CLass="monofont">get_position()</tt> and
<tt class="monofont">set_position().</tt>
</p>
</p>
<p>
<a naMe="idx1073747404"></a>
<A namE="idx1073747405"></a>
<p>
<tt ClasS="monofont">get_buffer()</TT>棤
Returns the current unpack data buffer as a string.
</P>
</p>
<p>
<a nAME="idx1073747406"></A>
<a namE="idx1073747407"></A>
<P>
<Tt claSS="monofont">done()</TT>棤
Indicates unpack completion. Raises an error exception if all
the data has not been unpacked.
</p>
</p>
</blockquote>
<p>In addition, every data type that can be packed with a Packer, can
be unpacked with an Unpacker. Unpacking methods are of the form
<tt class="monofont">unpack_type(),</Tt> and take no arguments. They return the
unpacked object.</p>
<BlocKquotE>
<p>
<a nAME="idx1073747408"></A>
<a namE="idx1073747409"></A>
<P>
<Tt claSS="monofont">unpack_float()</TT>棤
Unpacks a single-precision floating point number.
</p>
</p>
<p>
<a NAME="idx1073747410"></a>
<a name="idx1073747411"></a>
<p>
<tt class="monofont">unpack_double()</tt>棤
Unpacks a double-precision floating point number, similar to
<tt cLasS="monofont">unpack_float().</tt>
</p>
</P>
</blocKquoTE>
<P>In addition, the following methods unpack strings, bytes, and
opaque data:</P>
<blocKQUOte>
<p>
<a NAME="idx1073747412"></a>
<a naME="idx1073747413"></A>
<P>
<tt class="monofont">unpack_fstring(n)</tt>棤
Unpacks and returns a fixed length string.
<tt class="monofont">n</tt> is the number of characters expected. Padding with null
bytes to guaranteed 4 byte alignment is assumed.
</p>
</p>
<P>
<a nAme="idx1073747414"></a>
<A name="idx1073747415"></A>
<p>
<tt CLASs="monofont">unpack_fopaque(n)</tt>棤
Unpacks and returns a fixed length opaque data stream,
similar to <tT CLAss="monofont">unpack_fstring().</tt>
<A NAMe="idx1073747416"></a>
<a nAME="idx1073747417"></A>
<a name="idx1073747418"></a>
<a name="idx1073747419"></a>
<a name="idx1073747420"></a>
<a nAme="idx1073747421"></A>
<a naMe="idx1073747422"></a>
<a nAme="idx1073747423"></a>
<A NAMe="idx1073747424"></a>
<a nAME="idx1073747425"></A>
<a namE="idx1073747426"></A>
</P>
</P>
<p>
<a naME="idx1073747427"></A>
<A name="idx1073747428"></a>
<p>
<tt class="monofont">unpack_string()</tt>棤
Unpacks and returns a variable length string. The length of
the string is first unpacked as an unsigned integer, and then the string data
is unpacked with <tt claSs="monofont">unpack_fstring().</tT>
</p>
</p>
<p>
<A name="idx1073747429"></A>
<a naME="idx1073747430"></A>
<P>
<tt clASS="monofont">unpack_opaque()</Tt>棤
Unpacks and returns a variable length opaque data string,
similar to <tt cLASS="monofont">unpack_string().</tt>
</p>
</p>
<P>
<A NAme="idx1073747431"></a>
<a name="idx1073747432"></a>
<p>
<tt class="monofont">unpack_bytes()</tt>棤
Unpacks and returns a variable length byte stream, similar to
<tT clAss="monofont">unpack_string().</tT>
</p>
</p>
</blOckqUOTE>
<p>The following methods support unpacking arrays and lists:</p>
<blOCKQuote>
<P>
<A NAme="idx1073747433"></a>
<a NAME="idx1073747434"></a>
<p>
<tt class="monofont">unpack_list</tt>(<i>unpack_item</i>)棤
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 <tt class="monofont">1,</Tt> the item is unpacked and appended to the
list. A flag of <tT claSs="monofont">0</tt> indicates the end of the list.
<tT claSS="monofont">unpack_item</TT> is the function called to unpack the items.
<a namE="idx1073747435"></A>
<A Name="idx1073747436"></a>
<A NAMe="idx1073747437"></a>
<a nAME="idx1073747438"></A>
<a name="idx1073747439"></a>
<a name="idx1073747440"></a>
<a name="idx1073747441"></a>
<a nAme="idx1073747442"></A>
<a naMe="idx1073747443"></a>
<a nAme="idx1073747444"></a>
</P>
</P>
<P>
<A name="idx1073747445"></A>
<A NAme="idx1073747446"></a>
<p>
<TT CLass="monofont">unpack_farray</tT>(<I>n,
unpack_item</I>)棤
Unpacks and returns (as a list) a fixed length array of
homogeneous items. <Tt class="monofont">n</tt> is the number of list elements to
expect in the buffer. As stated previously, <tt class="monofont">unpack_item</tt> is
the function used to unpack each element.
</p>
</p>
<p>
<A naMe="idx1073747447"></a>
<a Name="idx1073747448"></a>
<P>
<tt cLASS="monofont">unpack_array</tt>(<i>unpack_item</i>)棤
Unpacks and returns a variable length list of homogeneous
items. First, the length of the list is unpacked as an unsigned integer, and
then each element is unpacked as in <TT CLass="monofont">unpack_farray()</tT>
previously.
<A NAme="idx1073747449"></a>
</p>
</P>
</BLOckquote>
<p>In the following example, we
<a name="idx1073747450"></a>
<a name="idx1073747451"></a>
<A naMe="idx1073747452"></a>pack a group of variables, unpacking them later.</p>
<Pre>
import xdrlib
def f_packer(name, author, month, year):
data = xdrlib.Packer()
data.pack_string(name)
data.pack_string(author)
data.pack_uint(month)
data.pack_uint(year)
packed = data.get_buffer()
return packed
def f_unpacker(packer):
data = xdrlib.Unpacker(packer)
return data
print "The original values are: 'Andre', 'Author', 10, 2000"
print
packed = f_packer('Andre', 'Author', 10, 2000)
print "The packed data is now defined by:", repr(packed)
print
print "And now, the original data again. (After unpacking it!)"
unpacked = f_unpacker(packed)
print repr(unpacked.unpack_string()), ", ", \
repr(unpacked.unpack_string()), ", ", \
unpacked.unpack_uint(), ", ", \
unpacked.unpack_uint()
unpacked.done()
The original values are: 'Andre', 'Author', 10, 2000
The packed data is now defined by:
'\000\000\000\005Andre\000\000\000\000\000\000\006Author\000\000\000\000\
000\012\000\000\007\320'
And now, the original data again. (After unpacking it!)
'Andre', 'Author', 10 , 2000
</prE>
<div CLASs="note"><p clASS="notetitle"><B>Note</b></p><p>
<p>If you are only handling simple data types and only with Python,
it is probably easier to just use the marshal module.
<A NAMe="idx1073747453"></a>
<a nAME="idx1073747454"></A>
<a name="idx1073747455"></a>
<a name="idx1073747456"></a>
<a name="idx1073747457"></a>
<a nAme="idx1073747458"></A>
<a naMe="idx1073747459"></a>
<a nAme="idx1073747460"></a>
<A NAMe="idx1073747461"></a>
<a nAME="idx1073747462"></A></p>
</p></diV>
<BR>
<Br>
<h4>
Exceptions</h4>
<p>Exceptions in this module are coded as class instances:</P>
<BLOckquote>
<p>
<a name="idx1073747463"></a>
<a name="idx1073747464"></a>
<P>
<tt ClasS="monofont">Error</tt>棤
This is the base exception class. Error has a single public
data member <tt ClasS="monofont">msg</TT> containing the description of the
error.
</P>
</p>
<p>
<a nAME="idx1073747465"></A>
<a namE="idx1073747466"></A>
<P>
<Tt claSS="monofont">ConversionError</TT>棤
This class is derived from Error. Contains no additional
instance variables.
</p>
</p>
</blockquote>
<p>Here is a simple example of how you would catch one of these
exceptions:</p>
<pre>
>>> import xdrlib
>>> data = xdrlib.Packer()
>>> try:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -