2 This module is a SOAP client using twisted's deferreds.
3 It uses the SOAPpy package.
5 @author: Raphael Slinckx
6 @copyright: Copyright 2005
8 @contact: U{raphael@slinckx.net<mailto:raphael@slinckx.net>}
14 import SOAPpy, logging
15 from SOAPpy.Config import Config
16 from twisted.web import client, error
21 class SoapError(Exception):
23 This is a SOAP error message, not an HTTP error message.
25 The content of this error is a SOAPpy structure representing the
32 Proxy for an url to which we send SOAP rpc calls.
34 def __init__(self, url, prefix):
36 Init the proxy, it will connect to the given url, using the
39 @param url: The url of the remote host to call
40 @param prefix: The namespace prefix to use, eg.
41 'urn:schemas-upnp-org:service:WANIPConnection:1'
43 logging.debug("Soap Proxy: '%s', prefix: '%s'", url, prefix)
47 def call(self, method, **kwargs):
49 Call the given remote method with the given arguments, as keywords.
51 Returns a deferred, called with SOAPpy structure representing
54 @param method: The method name to call, eg. 'GetExternalIP'
55 @param kwargs: The parameters of the call, as keywords
56 @return: A deferred called with the external ip address of this host
57 @rtype: L{twisted.internet.defer.Deferred}
59 payload = SOAPpy.buildSOAP(method=method, config=Config, namespace=self._prefix, kw=kwargs)
60 # Here begins the nasty hack
61 payload = payload.replace(
62 # Upnp wants s: instead of SOAP-ENV
63 'SOAP-ENV','s').replace(
64 # Doesn't seem to like these encoding stuff
65 'xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"', '').replace(
66 'SOAP-ENC:root="1"', '').replace(
67 # And it wants u: instead of ns1 namespace for arguments..
70 logging.debug("SOAP Payload:\n%s", payload)
72 return client.getPage(self._url, postdata=payload, method="POST",
73 headers={'content-type': 'text/xml', 'SOAPACTION': '%s#%s' % (self._prefix, method)}
74 ).addCallbacks(self._got_page, self._got_error)
76 def _got_page(self, result):
78 The http POST command was successful, we parse the SOAP
79 answer, and return it.
81 @param result: the xml content
83 parsed = SOAPpy.parseSOAPRPC(result)
85 logging.debug("SOAP Answer:\n%s", result)
86 logging.debug("SOAP Parsed Answer: %r", parsed)
90 def _got_error(self, res):
92 The HTTP POST command did not succeed, depending on the error type:
93 - it's a SOAP error, we parse it and return a L{SoapError}.
94 - it's another type of error (http, other), we raise it as is
96 logging.debug("SOAP Error:\n%s", res)
98 if isinstance(res.value, error.Error):
100 logging.debug("SOAP Error content:\n%s", res.value.response)
101 raise SoapError(SOAPpy.parseSOAPRPC(res.value.response)["detail"])
104 raise Exception(res.value)