File: node14.html

package info (click to toggle)
cherrypy 0.10-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 10,324 kB
  • ctags: 1,759
  • sloc: python: 14,411; sh: 6,915; perl: 2,472; makefile: 76
file content (234 lines) | stat: -rw-r--r-- 7,143 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>13. How to create an XML-RPC server with CherryPy</title>
<META NAME="description" CONTENT="13. How to create an XML-RPC server with CherryPy">
<META NAME="keywords" CONTENT="howto">
<META NAME="resource-type" CONTENT="document">
<META NAME="distribution" CONTENT="global">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="STYLESHEET" href="howto.css" type='text/css'>
<link rel="first" href="howto.html">
<link rel="contents" href="contents.html" title="Contents">

<LINK REL="next" HREF="node15.html">
<LINK REL="previous" HREF="node13.html">
<LINK REL="up" HREF="howto.html">
<LINK REL="next" HREF="node15.html">
<meta name='aesop' content='information'>
</head>
<body>
<DIV CLASS="navigation">
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td><A HREF="node13.html"><img src="../icons/previous.gif"
  border="0" height="32"
  alt="Previous Page" width="32"></A></td>
<td><A HREF="howto.html"><img src="../icons/up.gif"
  border="0" height="32"
  alt="Up One Level" width="32"></A></td>
<td><A HREF="node15.html"><img src="../icons/next.gif"
  border="0" height="32"
  alt="Next Page" width="32"></A></td>
<td align="center" width="100%">CherryPy HowTo</td>
<td><A HREF="node1.html"><img src="../icons/contents.gif"
  border="0" height="32"
  alt="Contents" width="32"></A></td>
<td><img src="../icons/blank.gif"
  border="0" height="32"
  alt="" width="32"></td>
<td><img src="../icons/blank.gif"
  border="0" height="32"
  alt="" width="32"></td>
</tr></table>
<b class="navlabel">Previous:</b> <a class="sectref" HREF="node13.html">12. How to create</A>
<b class="navlabel">Up:</b> <a class="sectref" HREF="howto.html">CherryPy HowTo</A>
<b class="navlabel">Next:</b> <a class="sectref" HREF="node15.html">14. How to make</A>
<br><hr>
</DIV>
<!--End of Navigation Panel-->
<!--Table of Child-Links-->
<A NAME="CHILD_LINKS"><STRONG>Subsections</STRONG></a>

<UL CLASS="ChildLinks">
<LI><A href="node14.html#SECTION0014100000000000000000">13.1 Basic Example</a>
<LI><A href="node14.html#SECTION0014200000000000000000">13.2 Multiple Servers</a>
</ul>
<!--End of Table of Child-Links-->
<HR>

<H1><A NAME="SECTION0014000000000000000000">
13. How to create an XML-RPC server with CherryPy</A>
</H1>
Writing an XML-RPC service with CherryPy is very easy. It works very much the same way as a regular web service.
It is base on the <var>xmlrpclib</var> module. This module is included with Python-2.2 and higher, but if you use a lower
version, you'll have to download it from <a class="url" href="http://www.pythonware.com">http://www.pythonware.com</a>.

<P>
All you have to do is add the keyword xmlrpc after the definition of the
mask or view, and before the colon. Now, that view or mask will be
available to XML-RPC requests. 

<P>

<H1><A NAME="SECTION0014100000000000000000">
13.1 Basic Example</A>
</H1>
Let's take an example. Just create a file called testXmlRpc.cpy
containing the following code: 

<P>
<div class="verbatim"><pre>
CherryClass Root:
view:
    def add(self, a, b, c) xmlrpc:
        # Return the sum of three numbers
        return a+b+c

CherryClass Xml_rpc:
view:
    def reverse(self, label) xmlrpc:
        # Reverse the characters of a string
        newStr=''
        for i in range(len(label)-1,-1,-1):
            newStr+=label[i]
        return newStr
</pre></div>

<P>
Also, create a configuration file (textXmlRpcServer.cfg) to tell the
server to accept XML-RPC requests: 

<P>
<div class="verbatim"><pre>
[server]
typeOfRequests=xmlRpc,web
</pre></div>

<P>
Compile the source file and start the server: you have an XML-RPC server
running. 

<P>
Now let's write a client. Just create a file called testXmlRpcClient.py
containing the following code: 

<P>
<div class="verbatim"><pre>
import xmlrpclib
testsvr = xmlrpclib.Server("http://localhost:8000")
print testsvr.add(1,2,3)
print testsvr.xml.rpc.reverse("I love cherry pie")
</pre></div>

<P>
Run the client and you should see: 

<P>
<div class="verbatim"><pre>
[remi@serveur]$ python xmlrpcTestClient.py
6
eip yrrehc evol I
[remi@serveur]$
</pre></div>

<P>

<H1><A NAME="SECTION0014200000000000000000">
13.2 Multiple Servers</A>
</H1>

<P>
CherryPy lets you define your XML-RPC server URIs any way you want. In
our example client above, we used:

<P>
<div class="verbatim"><pre>
testsvr = xmlrpclib.Server("http://localhost:8000")
print testsvr.xml.rpc.reverse("I love cherry pie")
</pre></div>

<P>
However, either of the following would work just as well.

<P>
<div class="verbatim"><pre>
testsvr = xmlrpclib.Server("http://localhost:8000/xml")
print testsvr.rpc.reverse("I love cherry pie")
</pre></div>

<P>
or

<P>
<div class="verbatim"><pre>
testsvr = xmlrpclib.Server("http://localhost:8000/xml/rpc")
print testsvr.reverse("I love cherry pie")
</pre></div>

<P>
Note that in these two examples, the <var>add</var> view in the <var>Root</var> CherryClass
would not be available to the XML-RPC client. This gives you the
flexibility to define your XML-RPC APIs to make different methods
available at different URIs.

<P>
PS: Note that you can also declare a whole CherryClass as xmlrpc. This is equivalent to declaring each mask and view of this
CherryClass as xmlrpc. For instance, the basic example of this HowTo could have been written as:

<P>
<div class="verbatim"><pre>
CherryClass Root xmlrpc:
view:
    def add(self, a, b, c):
        # Return the sum of three numbers
        return a+b+c

CherryClass Xml_rpc xmlrpc:
view:
    def reverse(self, label):
        # Reverse the characters of a string
        newStr=''
        for i in range(len(label)-1,-1,-1):
            newStr+=label[i]
        return newStr
</pre></div>

<P>

<DIV CLASS="navigation">
<p><hr>
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td><A HREF="node13.html"><img src="../icons/previous.gif"
  border="0" height="32"
  alt="Previous Page" width="32"></A></td>
<td><A HREF="howto.html"><img src="../icons/up.gif"
  border="0" height="32"
  alt="Up One Level" width="32"></A></td>
<td><A HREF="node15.html"><img src="../icons/next.gif"
  border="0" height="32"
  alt="Next Page" width="32"></A></td>
<td align="center" width="100%">CherryPy HowTo</td>
<td><A HREF="node1.html"><img src="../icons/contents.gif"
  border="0" height="32"
  alt="Contents" width="32"></A></td>
<td><img src="../icons/blank.gif"
  border="0" height="32"
  alt="" width="32"></td>
<td><img src="../icons/blank.gif"
  border="0" height="32"
  alt="" width="32"></td>
</tr></table>
<b class="navlabel">Previous:</b> <a class="sectref" HREF="node13.html">12. How to create</A>
<b class="navlabel">Up:</b> <a class="sectref" HREF="howto.html">CherryPy HowTo</A>
<b class="navlabel">Next:</b> <a class="sectref" HREF="node15.html">14. How to make</A>
<hr>
<span class="release-info">Release 0.10, documentation updated on 19 March 2004.</span>
</DIV>
<!--End of Navigation Panel-->
<ADDRESS>
See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
</ADDRESS>
</BODY>
</HTML>