📄 192.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<META NAME="Robots" content="INDEX,NOFOLLOW">
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<TITLE>Safari | Python Developer's Handbook -> XDR Data Exchange Format</TITLE>
<LINK REL="stylesheet" HREF="oreillyi/oreillyN.css">
</HEAD>
<BODY bgcolor="white" text="black" link="#990000" vlink="#990000" alink="#990000" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<table width="100%" cellpadding=5 cellspacing=0 border=0 class="navtopbg"><tr><td><font size="1"><p class="navtitle"><a href="8.html" class="navtitle">Web Development</a> > <a href="0672319942.html" class="navtitle">Python Developer's Handbook</a> > <a href="187.html" class="navtitle">13. Data Manipulation</a> > <span class="nonavtitle">XDR Data Exchange Format</span></p></font></td><td align="right" valign="top" nowrap><font size="1"><a href="main.asp?list" class="safnavoff">See All Titles</a></font></td></tr></table>
<TABLE width=100% bgcolor=white border=0 cellspacing=0 cellpadding=5><TR><TD>
<TABLE border=0 width="100%" cellspacing=0 cellpadding=0><TR><td align=left width="15%" class="headingsubbarbg"><a href="191.html" title="XML-RPC"><font size="1">< BACK</font></a></td><td align=center width="70%" class="headingsubbarbg"><font size="1"><a href="popanote.asp?pubui=oreilly&bookname=0672319942&snode=192" target="_blank" title="Make a public or private annnotation">Make Note</a> | <a href="192.html" title="Use a Safari bookmark to remember this section">Bookmark</a></font></td><td align=right width="15%" class="headingsubbarbg"><a href="193.html" title="Handling Other Markup Languages"><font size="1">CONTINUE ></font></a></td></TR></TABLE>
<a href="5%2F31%2F2002+4%3A46%3A15+PM.html" TABINDEX="-1"><img src=images/spacer.gif border=0 width=1 height=1></a><font color=white size=1>152015024128143245168232148039199167010047123209178152124239215162148042031165113061128045</font><a href="read3.asp?bookname=0672319942&snode=192&now=5%2F31%2F2002+4%3A46%3A15+PM" TABINDEX="-1"><img src=images/spacer.gif border=0 width=1 height=1></a><br>
<FONT>
<h3>
XDR Data Exchange Format</h3>
<p>XDR is best described as a standard for data description and
encoding. It uses a implicit typing language to describe intricate data formats
in a concise manner梟ote that this language is not a programming
language. Protocols such as Sun RPC (Remote Procedure Call) and the NFS
(Network File System, which was initially built on top of RPC) use XDR to
describe the format of their data because XDR is useful for transferring data
between different computer architectures. XDR has been used to communicate with
data between such diverse machines as the SUN WORKSTATION, VAX, IBM-PC, and
Cray. It is a very portable implementation. For more information, check
out</p>
<BLOCkquoTE>
<P>
<P>Internet standards桼FC 1014, External Data
Representation</p>
</p>
<p>
<p>
<A TARget="_blank" href="http://info.internet.isi.edu/in-notes/rfc/files/rfc1014.txt">http://info.internet.isi.edu/in-notes/rfc/files/rfc1014.txt</a></p>
</p>
</blockquote>
<H4><tt ClasS="monofont">
<a namE="idx1073747312"></a>
<a nAME="idx1073747313"></A>xdrlib</tt></h4>
<p>The <TT CLass="monofont">xdrlib</tT> module almost entirely supports the
<A NAme="idx1073747314"></a>
<a NAME="idx1073747315"></a><i>External Data Representation Standard
(XDR)</i> as described in RFC 1014, written by Sun Microsystems, Inc. on
June 1987. Therefore, it is used extensively in networked applications, mainly
the ones that need to handle RPC.</p>
<p>This module defines two exceptions, and two
<a name="idx1073747316"></a>classes梠ne for packing variables into XDR
representation, and another for unpacking from XDR representation:</p>
<blockquoTe>
<p>
<A namE="idx1073747317"></a>
<a naMe="idx1073747318"></a>
<p>
<TT CLass="monofont">Packer()</tT>棤
<TT Class="monofont">Packer</TT> is the class for packing data into
XDR representation. The <TT clasS="monofont">Packer</TT> class is instantiated with no
arguments.
</P>
</p>
<p>
<a name="idx1073747319"></a>
<a name="idx1073747320"></a>
<p>
<tt claSs="monofont">Unpacker(data)</tT>棤
<tt cLass="monofont">Unpacker</tT> is the complementary class, which
unpacks XDR data values from a string buffer. The input buffer is given as
data.
</p>
</p>
</bLOCKquotE>
<H4>
Packer Objects</H4>
<P>Packer instances have the following
<a namE="idx1073747321"></A>methods:</P>
<BlockQUOTe>
<p>
<a name="idx1073747322"></a>
<a name="idx1073747323"></a>
<p>
<tt claSs="monofont">get_buffer()</tT>棤
Returns the current pack buffer as a string.
</p>
</p>
<p>
<A name="idx1073747324"></A>
<a naME="idx1073747325"></A>
<P>
<tt clASS="monofont">reset()</Tt>棤
Resets the pack buffer to the empty string.
</p>
</p>
</bLOCKquotE>
<P>In general, you can pack any of the most common XDR data types by
calling the appropriate <TT class="monofont">pack_type()</tt> method. Each method
takes a single argument, the value to pack. The following simple data type
packing methods are supported:
<tt class="monofont">pack_uint(),</tt><tt clAss="monofont">pack_int(),</Tt><tt Class="monofont">pack_enum(),</Tt><tt CLASs="monofont">pack_bool(),</tt><tT CLAss="monofont">pack_uhyper(),</tt>
and <TT CLass="monofont">pack_hyper().</tT> The following methods support
<A NAme="idx1073747326"></a>
<a name="idx1073747327"></a>
<a name="idx1073747328"></a>floating point number packing.
<a name="idx1073747329"></A>
<a nAme="idx1073747330"></a>
<A name="idx1073747331"></A>
<a naME="idx1073747332"></A>
<A name="idx1073747333"></A>
<A NAme="idx1073747334"></a>
<a NAME="idx1073747335"></a>
<a naME="idx1073747336"></A>
<A name="idx1073747337"></a>
<a name="idx1073747338"></a>
<a name="idx1073747339"></a></p>
<blOckQuotE>
<p>
<a naMe="idx1073747340"></a>
<a NAME="idx1073747341"></a>
<p>
<tt CLASs="monofont">pack_float(value)</tt>棤
Packs the single-precision floating point number
value.
</p>
</P>
<P>
<A Name="idx1073747342"></a>
<A NAMe="idx1073747343"></a>
<p>
<tt class="monofont">pack_double(value)</tt>棤
Packs the double-precision floating point number
value.
</p>
</p>
</blockqUotE>
<p>The following methods support packing
<a nAme="idx1073747344"></a>
<a Name="idx1073747345"></A>strings,
<A NAme="idx1073747346"></a>
<a NAME="idx1073747347"></a>bytes, and
<a naME="idx1073747348"></A>
<A name="idx1073747349"></A>
<A NAme="idx1073747350"></a>opaque data:</p>
<blockquote>
<p>
<a name="idx1073747351"></A>
<a nAme="idx1073747352"></a>
<P>
<tt clAss="monofont">pack_fstring(n, s)</tT>棤
Packs a fixed length string,
<TT Class="monofont">s.</TT><TT clasS="monofont">n</TT> is the length of the string, but it
is not packed into the data buffer. The string is padded with null bytes if
necessary to guarantee 4 byte alignment.
</P>
</p>
<p>
<a nAME="idx1073747353"></A>
<a name="idx1073747354"></a>
<p>
<tt class="monofont">pack_fopaque(n, data)</tt>棤
Packs a fixed length opaque data stream, similar to
<tt clAss="monofont">pack_fstring().</Tt>
</p>
</p>
<P>
<a namE="idx1073747355"></a>
<a nAME="idx1073747356"></A>
<p>
<tt cLASS="monofont">pack_string(s)</tt>棤
Packs a variable length string, <tt CLASs="monofont">s.</tt> The
length of the string is first packed as an unsigned integer, and then the
string data is packed with <tT CLAss="monofont">pack_fstring().</tt>
</p>
</p>
<p>
<a name="idx1073747357"></a>
<a name="idx1073747358"></a>
<p>
<Tt cLass="monofont">pack_opaque(data)</Tt>棤
Packs a variable length opaque data string, similar to
<tt cLass="monofont">pack_string().</TT>
</P>
</P>
<p>
<a naME="idx1073747359"></A>
<A name="idx1073747360"></A>
<P>
<TT clasS="monofont">pack_bytes(bytes)</TT>棤
Packs a variable length byte stream, similar to
<Tt class="monofont">pack_string().</tt>
<a name="idx1073747361"></a>
<a name="idx1073747362"></a>
<A naMe="idx1073747363"></a>
<a Name="idx1073747364"></a>
<A namE="idx1073747365"></A>
<A Name="idx1073747366"></a>
<A NAMe="idx1073747367"></a>
</p>
</p>
</BLOCkquoTE>
<P>The following methods support packing
<A name="idx1073747368"></a>
<a name="idx1073747369"></a>arrays and
<a name="idx1073747370"></a>
<a naMe="idx1073747371"></a>lists:</P>
<bloCkquoTe>
<p>
<a NAME="idx1073747372"></a>
<a naME="idx1073747373"></A>
<P>
<tt clASS="monofont">pack_list(list, pack_item)</Tt>棤
Packs a list of homogeneous items. This method is useful for
lists with an indeterminate size; that is, 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.
<tt cLASS="monofont">pack_item</tt> is the function called to pack the individual
item. At the end of the list, an unsigned integer 0 is packed.
<a name="idx1073747374"></a>
<a name="idx1073747375"></a>
<a name="idx1073747376"></a>
<A naMe="idx1073747377"></a>
<a Name="idx1073747378"></a>
<A namE="idx1073747379"></A>
<A Name="idx1073747380"></a>
<A NAMe="idx1073747381"></a>
<a nAME="idx1073747382"></A>
<a namE="idx1073747383"></A>
<A Name="idx1073747384"></a>
</p>
</p>
<p>
<a name="idx1073747385"></a>
<a name="idx1073747386"></a>
<p>
<Tt cLass="monofont">pack_farray(n, array,
pack_item)</Tt>棤
Packs a fixed length list (array) of homogeneous items.
<tt cLass="monofont">n</TT> is the length of the list; it is not packed into the
buffer, but a <TT clasS="monofont">ValueError</TT> exception is raised if len(array)
is not equal to <Tt claSS="monofont">n.</TT> As stated previously,
<tt clASS="monofont">pack_item</Tt> is the function used to pack each element.
</p>
</p>
<p>
<a name="idx1073747387"></a>
<a name="idx1073747388"></a>
<p>
<tt cLasS="monofont">pack_array(list,
pack_item)</tt>棤
Packs a variable length list of homogeneous items. First, the
length of the list is packed as an unsigned integer, and then each element is
packed as in <tT clasS="monofont">pack_farray()</tt> stated previously.
<a NAME="idx1073747389"></a>
<a naME="idx1073747390"></A>
<A name="idx1073747391"></A>
<A NAme="idx1073747392"></a>
<a NAME="idx1073747393"></a>
<a name="idx1073747394"></a>
<a name="idx1073747395"></a>
<a name="idx1073747396"></a>
</p>
</P>
</blOckqUote>
<h4>
Unpacker Objects</H4>
<p>The Unpacker class offers the following
<a nAME="idx1073747397"></A>methods:</p>
<bloCKQUote>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -