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

Skip to content

Commit 95be7ff

Browse files
committed
merge from 3.3
Issue #19082: Working xmlrpc.server and xmlrpc.client examples. Both in modules and in documentation.
2 parents 8666e65 + 939e2db commit 95be7ff

4 files changed

Lines changed: 86 additions & 5 deletions

File tree

Doc/library/xmlrpc.server.rst

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,70 @@ server::
184184
# Print list of available methods
185185
print(s.system.listMethods())
186186

187+
The following example included in `Lib/xmlrpc/server.py` module shows a server
188+
allowing dotted names and registering a multicall function.
189+
190+
.. warning::
191+
192+
Enabling the *allow_dotted_names* option allows intruders to access your
193+
module's global variables and may allow intruders to execute arbitrary code on
194+
your machine. Only use this example only within a secure, closed network.
195+
196+
::
197+
198+
import datetime
199+
200+
class ExampleService:
201+
def getData(self):
202+
return '42'
203+
204+
class currentTime:
205+
@staticmethod
206+
def getCurrentTime():
207+
return datetime.datetime.now()
208+
209+
server = SimpleXMLRPCServer(("localhost", 8000))
210+
server.register_function(pow)
211+
server.register_function(lambda x,y: x+y, 'add')
212+
server.register_instance(ExampleService(), allow_dotted_names=True)
213+
server.register_multicall_functions()
214+
print('Serving XML-RPC on localhost port 8000')
215+
try:
216+
server.serve_forever()
217+
except KeyboardInterrupt:
218+
print("\nKeyboard interrupt received, exiting.")
219+
server.server_close()
220+
sys.exit(0)
221+
222+
This ExampleService demo can be invoked from the command line::
223+
224+
python -m xmlrpc.server
225+
226+
227+
The client that interacts with the above server is included in
228+
`Lib/xmlrpc/client.py`::
229+
230+
server = ServerProxy("http://localhost:8000")
231+
232+
try:
233+
print(server.currentTime.getCurrentTime())
234+
except Error as v:
235+
print("ERROR", v)
236+
237+
multi = MultiCall(server)
238+
multi.getData()
239+
multi.pow(2,9)
240+
multi.add(1,2)
241+
try:
242+
for response in multi():
243+
print(response)
244+
except Error as v:
245+
print("ERROR", v)
246+
247+
This client which interacts with the demo XMLRPC server can be invoked as::
248+
249+
python -m xmlrpc.client
250+
187251

188252
CGIXMLRPCRequestHandler
189253
-----------------------

Lib/xmlrpc/client.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1461,18 +1461,18 @@ def __call__(self, attr):
14611461

14621462
# simple test program (from the XML-RPC specification)
14631463

1464-
# server = ServerProxy("http://localhost:8000") # local server
1465-
server = ServerProxy("http://time.xmlrpc.com/RPC2")
1464+
# local server, available from Lib/xmlrpc/server.py
1465+
server = ServerProxy("http://localhost:8000")
14661466

14671467
try:
14681468
print(server.currentTime.getCurrentTime())
14691469
except Error as v:
14701470
print("ERROR", v)
14711471

1472-
# The server at xmlrpc.com doesn't seem to support multicall anymore.
14731472
multi = MultiCall(server)
1474-
multi.currentTime.getCurrentTime()
1475-
multi.currentTime.getCurrentTime()
1473+
multi.getData()
1474+
multi.pow(2,9)
1475+
multi.add(1,2)
14761476
try:
14771477
for response in multi():
14781478
print(response)

Lib/xmlrpc/server.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -960,10 +960,24 @@ def __init__(self):
960960

961961

962962
if __name__ == '__main__':
963+
import datetime
964+
965+
class ExampleService:
966+
def getData(self):
967+
return '42'
968+
969+
class currentTime:
970+
@staticmethod
971+
def getCurrentTime():
972+
return datetime.datetime.now()
973+
963974
server = SimpleXMLRPCServer(("localhost", 8000))
964975
server.register_function(pow)
965976
server.register_function(lambda x,y: x+y, 'add')
977+
server.register_instance(ExampleService(), allow_dotted_names=True)
978+
server.register_multicall_functions()
966979
print('Serving XML-RPC on localhost port 8000')
980+
print('It is advisable to run this example server within a secure, closed network.')
967981
try:
968982
server.serve_forever()
969983
except KeyboardInterrupt:

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ Core and Builtins
2525
Library
2626
-------
2727

28+
- Issue #19082: Working xmlrpc.server and xmlrpc.client examples. Both in
29+
modules and in documentation. Initial patch contributed by Vajrasky Kok.
30+
2831
- Issue #20138: The wsgiref.application_uri() and wsgiref.request_uri()
2932
functions now conform to PEP 3333 when handle non-ASCII URLs.
3033

0 commit comments

Comments
 (0)