old-www/HOWTO/XML-RPC-HOWTO/xmlrpc-howto-python.html

218 lines
3.5 KiB
HTML

<HTML
><HEAD
><TITLE
>Using XML-RPC with Python</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.63
"><LINK
REL="HOME"
TITLE="XML-RPC HOWTO"
HREF="index.html"><LINK
REL="PREVIOUS"
TITLE="Using XML-RPC with Perl"
HREF="xmlrpc-howto-perl.html"><LINK
REL="NEXT"
TITLE="Using XML-RPC with C and C++"
HREF="xmlrpc-howto-c.html"></HEAD
><BODY
CLASS="section"
BGCOLOR="#FFFFFF"
TEXT="#000000"
LINK="#0000FF"
VLINK="#840084"
ALINK="#0000FF"
><DIV
CLASS="NAVHEADER"
><TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="3"
ALIGN="center"
>XML-RPC HOWTO</TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="bottom"
><A
HREF="xmlrpc-howto-perl.html"
>Prev</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
></TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="xmlrpc-howto-c.html"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="section"
><H1
CLASS="section"
><A
NAME="xmlrpc-howto-python"
>7. Using XML-RPC with Python</A
></H1
><P
>Fredrik Lundh has provided an excellent <A
HREF="http://www.pythonware.com/products/xmlrpc/"
TARGET="_top"
>XML-RPC library for
Python</A
>.</P
><P
>To install, download the latest version. You can either stick the
<TT
CLASS="filename"
>*.py</TT
> files in the same directory as your Python
code, or you can install them in your system's Python directory.</P
><P
>RedHat 6.2 users can type the following:</P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="programlisting"
>bash$ mkdir xmlrpclib-0.9.8
bash$ cd xmlrpclib-0.9.8
bash$ unzip ../xmlrpc-0.9.8-990621.zip
bash$ python
python&#62; import xmlrpclib
python&#62; import xmlrpcserver
python&#62; <B
CLASS="keycap"
>Control-D</B
>
bash$ su -c 'cp *.py *.pyc /usr/lib/python1.5/'</PRE
></TD
></TR
></TABLE
><P
>We <TT
CLASS="literal"
>import</TT
> two of the <TT
CLASS="filename"
>*.py</TT
>
files to trick Python into compiling them. Users of other platforms
should consult their Python documentation.</P
><P
>For more Python examples, see the article <A
HREF="http://www.oreillynet.com/pub/a/python/2001/01/17/xmlrpcserver.html"
TARGET="_top"
>XML-RPC:
It Works Both Ways</A
> on the O'Reilly Network.</P
><DIV
CLASS="section"
><H2
CLASS="section"
><A
NAME="xmlrpc-howto-python-client"
>7.1. A Python Client</A
></H2
><P
>The following program shows how to call an XML-RPC server from
Python:</P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="programlisting"
>import xmlrpclib
# Create an object to represent our server.
server_url = 'http://xmlrpc-c.sourceforge.net/api/sample.php';
server = xmlrpclib.Server(server_url);
# Call the server and get our result.
result = server.sample.sumAndDifference(5, 3)
print "Sum:", result['sum']
print "Difference:", result['difference']</PRE
></TD
></TR
></TABLE
></DIV
></DIV
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="xmlrpc-howto-perl.html"
>Prev</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="index.html"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="xmlrpc-howto-c.html"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>Using XML-RPC with Perl</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
>&nbsp;</TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>Using XML-RPC with C and C++</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>