File: xmlrpc.py

package info (click to toggle)
python-cyclone 1.1-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,388 kB
  • ctags: 1,372
  • sloc: python: 8,823; sh: 183; makefile: 13; sql: 12
file content (117 lines) | stat: -rw-r--r-- 3,962 bytes parent folder | download | duplicates (2)
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
# coding: utf-8
#
# Copyright 2010 Alexandre Fiori
# based on the original Tornado by Facebook
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

"""Server-side implementation of the XML-RPC protocol.

`XML-RPC <http://en.wikipedia.org/wiki/XML-RPC>`_ is a remote procedure call
protocol which uses XML to encode its calls and HTTP as a transport mechanism.

For more information, check out the `RPC demo
<https://github.com/fiorix/cyclone/tree/master/demos/rpc>`_.
"""

import xmlrpclib

from twisted.internet import defer
from cyclone.web import RequestHandler


class XmlrpcRequestHandler(RequestHandler):
    """Subclass this class and define xmlrpc_* to make a handler.

    Example::

        class MyRequestHandler(XmlrpcRequestHandler):
            allowNone = True

            def xmlrpc_echo(self, text):
                return text

            def xmlrpc_sort(self, items):
                return sorted(items)

            @defer.inlineCallbacks
            def xmlrpc_geoip_lookup(self, address):
                response = yield cyclone.httpclient.fetch(
                    "http://freegeoip.net/xml/%s" % address.encode("utf-8"))
                defer.returnValue(response.body)
    """

    FAILURE = 8002
    NOT_FOUND = 8001
    separator = "."
    allowNone = False

    def post(self):
        self._auto_finish = False
        self.set_header("Content-Type", "text/xml")
        try:
            args, functionPath = xmlrpclib.loads(self.request.body)
        except Exception, e:
            f = xmlrpclib.Fault(self.FAILURE,
                                "Can't deserialize input: %s" % e)
            self._cbRender(f)
        else:
            try:
                function = self._getFunction(functionPath)
            except xmlrpclib.Fault, f:
                self._cbRender(f)
            else:
                d = defer.maybeDeferred(function, *args)
                d.addCallback(self._cbRender)
                d.addErrback(self._ebRender)

    def _getFunction(self, functionPath):
        if functionPath.find(self.separator) != -1:
            prefix, functionPath = functionPath.split(self.separator, 1)
            handler = self.getSubHandler(prefix)
            if handler is None:
                raise xmlrpclib.Fault(self.NOT_FOUND,
                    "no such subHandler %s" % prefix)
            return self._getFunction(functionPath)

        f = getattr(self, "xmlrpc_%s" % functionPath, None)
        if f is None:
            raise xmlrpclib.Fault(self.NOT_FOUND,
                "function %s not found" % functionPath)
        elif not callable(f):
            raise xmlrpclib.Fault(self.NOT_FOUND,
                "function %s not callable" % functionPath)
        else:
            return f

    def _cbRender(self, result):
        if not isinstance(result, xmlrpclib.Fault):
            result = (result,)

        try:
            s = xmlrpclib.dumps(result,
                methodresponse=True, allow_none=self.allowNone)
        except Exception, e:
            f = xmlrpclib.Fault(self.FAILURE, "can't serialize output: %s" % e)
            s = xmlrpclib.dumps(f,
                methodresponse=True, allow_none=self.allowNone)

        self.finish(s)

    def _ebRender(self, failure):
        if isinstance(failure.value, xmlrpclib.Fault):
            s = failure.value
        else:
            s = xmlrpclib.Fault(self.FAILURE, "error")

        self.finish(xmlrpclib.dumps(s, methodresponse=True))