Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ab807e8

Browse files
committed
Make the example server code clearer; add the corresponding example client. [Bugfix candidate]
1 parent 9cc5cb7 commit ab807e8

1 file changed

Lines changed: 36 additions & 5 deletions

File tree

Doc/lib/libsimplexmlrpc.tex

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,18 +88,49 @@ \subsection{SimpleXMLRPCServer Objects \label{simple-xmlrpc-servers}}
8888
Example:
8989

9090
\begin{verbatim}
91-
class MyFuncs:
92-
def div(self, x, y) : return x // y
93-
91+
from SimpleXMLRPCServer import SimpleXMLRPCServer
9492
93+
# Create server
9594
server = SimpleXMLRPCServer(("localhost", 8000))
96-
server.register_function(pow)
97-
server.register_function(lambda x,y: x+y, 'add')
9895
server.register_introspection_functions()
96+
97+
# Register pow() function; this will use the value of
98+
# pow.__name__ as the name, which is just 'pow'.
99+
server.register_function(pow)
100+
101+
# Register a function under a different name
102+
def adder_function(x,y):
103+
return x + y
104+
server.register_function(adder_function, 'add')
105+
106+
# Register an instance; all the methods of the instance are
107+
# published as XML-RPC methods (in this case, just 'div').
108+
class MyFuncs:
109+
def div(self, x, y):
110+
return x // y
111+
99112
server.register_instance(MyFuncs())
113+
114+
# Run the server's main loop
100115
server.serve_forever()
101116
\end{verbatim}
102117

118+
The following client code will call the methods made available by
119+
the preceding server:
120+
121+
\begin{verbatim}
122+
import xmlrpclib
123+
124+
s = xmlrpclib.Server('http://localhost:8000')
125+
print s.pow(2,3) # Returns 2**3 = 8
126+
print s.add(2,3) # Returns 5
127+
print s.div(5,2) # Returns 5//2 = 2
128+
129+
# Print list of available methods
130+
print s.system.listMethods()
131+
\end{verbatim}
132+
133+
103134
\subsection{CGIXMLRPCRequestHandler}
104135

105136
The \class{CGIXMLRPCRequestHandler} class can be used to

0 commit comments

Comments
 (0)