/usr/share/pyshared/zope/app/publisher/xmlrpc/README.txt is in python-zope.app.publisher 3.10.2-0ubuntu4.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 | XML-RPC views
=============
XML-RPC Methods
---------------
There are two ways to write XML-RPC views. You can write views that
provide "methods" for other objects, and you can write views that have
their own methods. Let's look at the former case first, since it's a
little bit simpler.
Let's write a view that returns a folder listing:
>>> class FolderListing:
... def contents(self):
... return list(self.context.keys())
Now we'll register it as a view:
>>> from zope.configuration import xmlconfig
>>> ignored = xmlconfig.string("""
... <configure
... xmlns="http://namespaces.zope.org/zope"
... xmlns:xmlrpc="http://namespaces.zope.org/xmlrpc"
... >
... <!-- We only need to do this include in this example,
... Normally the include has already been done for us. -->
... <include package="zope.app.publisher.xmlrpc" file="meta.zcml" />
...
... <xmlrpc:view
... for="zope.site.interfaces.IFolder"
... methods="contents"
... class="zope.app.publisher.xmlrpc.README.FolderListing"
... permission="zope.ManageContent"
... />
... </configure>
... """)
Now, we'll add some items to the root folder:
>>> print http(r"""
... POST /@@contents.html HTTP/1.1
... Authorization: Basic bWdyOm1ncnB3
... Content-Length: 73
... Content-Type: application/x-www-form-urlencoded
...
... type_name=BrowserAdd__zope.site.folder.Folder&new_value=f1""")
HTTP/1.1 303 See Other
...
>>> print http(r"""
... POST /@@contents.html HTTP/1.1
... Authorization: Basic bWdyOm1ncnB3
... Content-Length: 73
... Content-Type: application/x-www-form-urlencoded
...
... type_name=BrowserAdd__zope.site.folder.Folder&new_value=f2""")
HTTP/1.1 303 See Other
...
And call our xmlrpc method:
>>> from zope.app.testing.xmlrpc import ServerProxy
>>> proxy = ServerProxy("http://mgr:mgrpw@localhost/")
>>> proxy.contents()
['f1', 'f2']
Note that we get an unauthorized error if we don't supply authentication
credentials:
>>> proxy = ServerProxy("http://localhost/")
>>> proxy.contents()
Traceback (most recent call last):
ProtocolError: <ProtocolError for localhost/: 401 401 Unauthorized>
Named XML-RPC Views
-------------------
Now let's look at views that have their own methods or other
subobjects. Views that have their own methods have names that appear
in URLs and they get traversed to get to their methods, as in::
.../somefolder/listing/contents
To make this possible, the view has to support traversal, so that,
when it is traversed, it traverses to its attributes. To support
traversal, you can implement or provide an adapter to
`zope.publisher.interfaces.IPublishTraverse`. It's actually better to
provide an adapter so that accesses to attributes during traversal are
mediated by the security machinery. (Object methods are always bound
to unproxied objects, but adapters are bound to proxied objects unless
they are trusted adapters.)
The 'zope.app.publisher.xmlrpc' package provides a base class,
`MethodPublisher`, that provides the necessary traversal support. In
particulat, it has an adapter that simply traverses to attributes.
If an XML-RPC view isn't going to be public, then it also has to
implement 'zope.location.ILocation' so that security grants can be
acquired for it, at least with Zope's default security policy. The
`MethodPublisher` class does that too.
Let's modify our view class to use `MethodPublisher`:
>>> from zope.app.publisher.xmlrpc import MethodPublisher
>>> class FolderListing(MethodPublisher):
...
... def contents(self):
... return list(self.context.keys())
Note that `MethodPublisher` also provides a suitable `__init__`
method, so we don't need one any more. This time, we'll register it
as as a named view:
>>> ignored = xmlconfig.string("""
... <configure
... xmlns="http://namespaces.zope.org/zope"
... xmlns:xmlrpc="http://namespaces.zope.org/xmlrpc"
... >
... <!-- We only need to do this include in this example,
... Normally the include has already been done for us. -->
... <include package="zope.app.publisher.xmlrpc" file="meta.zcml" />
...
... <xmlrpc:view
... name="listing"
... for="zope.site.folder.IFolder"
... methods="contents"
... class="zope.app.publisher.xmlrpc.README.FolderListing"
... permission="zope.ManageContent"
... />
... </configure>
... """)
Now, when we access the `contents`, we do so through the listing view:
>>> proxy = ServerProxy("http://mgr:mgrpw@localhost/listing/")
>>> proxy.contents()
['f1', 'f2']
>>> proxy = ServerProxy("http://mgr:mgrpw@localhost/")
>>> proxy.listing.contents()
['f1', 'f2']
as before, we will get an error if we don't supply credentials:
>>> proxy = ServerProxy("http://localhost/listing/")
>>> proxy.contents()
Traceback (most recent call last):
ProtocolError: <ProtocolError for localhost/listing/: 401 401 Unauthorized>
Parameters
----------
Of course, XML-RPC views can take parameters, too:
>>> class ParameterDemo:
... def __init__(self, context, request):
... self.context = context
... self.request = request
...
... def add(self, first, second):
... return first + second
Now we'll register it as a view:
>>> from zope.configuration import xmlconfig
>>> ignored = xmlconfig.string("""
... <configure
... xmlns="http://namespaces.zope.org/zope"
... xmlns:xmlrpc="http://namespaces.zope.org/xmlrpc"
... >
... <!-- We only need to do this include in this example,
... Normally the include has already been done for us. -->
... <include package="zope.app.publisher.xmlrpc" file="meta.zcml" />
...
... <xmlrpc:view
... for="zope.site.interfaces.IFolder"
... methods="add"
... class="zope.app.publisher.xmlrpc.README.ParameterDemo"
... permission="zope.ManageContent"
... />
... </configure>
... """)
Then we can issue a remote procedure call with a parameter and get
back, surprise!, the sum:
>>> proxy = ServerProxy("http://mgr:mgrpw@localhost/")
>>> proxy.add(20, 22)
42
Faults
------
If you need to raise an error, the prefered way to do it is via an
`xmlrpclib.Fault`:
>>> import xmlrpclib
>>> class FaultDemo:
... def __init__(self, context, request):
... self.context = context
... self.request = request
...
... def your_fault(self):
... return xmlrpclib.Fault(42, "It's your fault!")
Now we'll register it as a view:
>>> from zope.configuration import xmlconfig
>>> ignored = xmlconfig.string("""
... <configure
... xmlns="http://namespaces.zope.org/zope"
... xmlns:xmlrpc="http://namespaces.zope.org/xmlrpc"
... >
... <!-- We only need to do this include in this example,
... Normally the include has already been done for us. -->
... <include package="zope.app.publisher.xmlrpc" file="meta.zcml" />
...
... <xmlrpc:view
... for="zope.site.interfaces.IFolder"
... methods="your_fault"
... class="zope.app.publisher.xmlrpc.README.FaultDemo"
... permission="zope.ManageContent"
... />
... </configure>
... """)
Now, when we call it, we get a proper XML-RPC fault:
>>> proxy = ServerProxy("http://mgr:mgrpw@localhost/")
>>> proxy.your_fault()
Traceback (most recent call last):
Fault: <Fault 42: "It's your fault!">
DateTime values
---------------
Unfortunately, `xmlrpclib` does not support Python 2.3's new
`datetime.datetime` class (it should be made to, really). DateTime
values need to be encoded as `xmlrpclib.DateTime` instances:
>>> import xmlrpclib
>>> class DateTimeDemo:
... def __init__(self, context, request):
... self.context = context
... self.request = request
...
... def epoch(self):
... return xmlrpclib.DateTime("19700101T01:00:01")
Now we'll register it as a view:
>>> from zope.configuration import xmlconfig
>>> ignored = xmlconfig.string("""
... <configure
... xmlns="http://namespaces.zope.org/zope"
... xmlns:xmlrpc="http://namespaces.zope.org/xmlrpc"
... >
... <!-- We only need to do this include in this example,
... Normally the include has already been done for us. -->
... <include package="zope.app.publisher.xmlrpc" file="meta.zcml" />
...
... <xmlrpc:view
... for="zope.site.interfaces.IFolder"
... methods="epoch"
... class="zope.app.publisher.xmlrpc.README.DateTimeDemo"
... permission="zope.ManageContent"
... />
... </configure>
... """)
Now, when we call it, we get a DateTime value
>>> proxy = ServerProxy("http://mgr:mgrpw@localhost/")
>>> proxy.epoch()
<DateTime u'19700101T01:00:01' at -4bcac114>
Protecting XML/RPC views with class-based permissions
-----------------------------------------------------
When setting up an XML/RPC view with no permission, the permission check is
deferred to the class that provides the view's implementation:
>>> class ProtectedView(object):
... def public(self):
... return u'foo'
... def protected(self):
... return u'bar'
>>> from zope.configuration import xmlconfig
>>> ignored = xmlconfig.string("""
... <configure
... xmlns="http://namespaces.zope.org/zope"
... xmlns:xmlrpc="http://namespaces.zope.org/xmlrpc"
... >
... <!-- We only need to do this include in this example,
... Normally the include has already been done for us. -->
... <include package="zope.app.publisher.xmlrpc" file="meta.zcml" />
... <include package="zope.security" file="meta.zcml" />
...
... <class class="zope.app.publisher.xmlrpc.README.ProtectedView">
... <require permission="zope.ManageContent"
... attributes="protected" />
... <allow attributes="public" />
... </class>
...
... <xmlrpc:view
... name="index"
... for="zope.site.interfaces.IFolder"
... methods="public protected"
... class="zope.app.publisher.xmlrpc.README.ProtectedView"
... />
... </configure>
... """)
An unauthenticated user can access the public method, but not the protected
one:
>>> proxy = ServerProxy("http://usr:usrpw@localhost/index", handleErrors=False)
>>> proxy.public()
'foo'
>>> proxy.protected() # doctest: +NORMALIZE_WHITESPACE
Traceback (most recent call last):
Unauthorized: (<zope.app.publisher.xmlrpc.metaconfigure.ProtectedView
object at 0x...>, 'protected', 'zope.ManageContent')
As a manager, we can access both:
>>> proxy = ServerProxy("http://mgr:mgrpw@localhost/index")
>>> proxy.public()
'foo'
>>> proxy.protected()
'bar'
Handling errors with the ServerProxy
------------------------------------
Our server proxy for functional testing also supports getting the original
errors from Zope by not handling the errors in the publisher:
>>> class ExceptionDemo:
... def __init__(self, context, request):
... self.context = context
... self.request = request
...
... def your_exception(self):
... raise Exception("Something went wrong!")
Now we'll register it as a view:
>>> from zope.configuration import xmlconfig
>>> ignored = xmlconfig.string("""
... <configure
... xmlns="http://namespaces.zope.org/zope"
... xmlns:xmlrpc="http://namespaces.zope.org/xmlrpc"
... >
... <!-- We only need to do this include in this example,
... Normally the include has already been done for us. -->
... <include package="zope.app.publisher.xmlrpc" file="meta.zcml" />
...
... <xmlrpc:view
... for="zope.site.interfaces.IFolder"
... methods="your_exception"
... class="zope.app.publisher.xmlrpc.README.ExceptionDemo"
... permission="zope.ManageContent"
... />
... </configure>
... """)
Now, when we call it, we get an XML-RPC fault:
>>> proxy = ServerProxy("http://mgr:mgrpw@localhost/")
>>> proxy.your_exception()
Traceback (most recent call last):
Fault: <Fault -1: 'Unexpected Zope exception: Exception: Something went wrong!'>
We can also give the parameter `handleErrors` to have the errors not be
handled:
>>> proxy = ServerProxy("http://mgr:mgrpw@localhost/", handleErrors=False)
>>> proxy.your_exception()
Traceback (most recent call last):
Exception: Something went wrong!
|