XML-RPC 编码是个坑,只支持 ASCII 字符串(或二进制缓冲块)。为了安全,只能用 xmlrpclib.Binary 把数据包装一下了。
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 | Python 2.7.5 (default, May 15 2013, 22:43:36) [MSC v.1500 32 bit (Intel)] on win32 Type "copyright", "credits" or "license()" for more information. >>> from SimpleXMLRPCServer import SimpleXMLRPCServer >>> import xmlrpclib >>> def a(): return xmlrpclib.Binary({'a':1}) >>> server = SimpleXMLRPCServer(("localhost", 8000)) >>> print "Listening on port 8000..." Listening on port 8000... >>> server.register_function(python_logo, 'python_logo') Traceback (most recent call last): File "<pyshell#7>", line 1, in <module> server.register_function(python_logo, 'python_logo') NameError: name 'python_logo' is not defined >>> server.register_function(a, 'a') >>> server.serve_forever() 127.0.0.1 - - [22/Apr/2014 22:36:32] "POST / HTTP/1.1" 200 - Traceback (most recent call last): File "<pyshell#9>", line 1, in <module> server.serve_forever() File "C:\Python27\lib\SocketServer.py", line 236, in serve_forever poll_interval) File "C:\Python27\lib\SocketServer.py", line 155, in _eintr_retry return func(*args) KeyboardInterrupt >>> import json >>> def a(): return xmlrpclib.Binary(json.dumps({'a':1})) >>> server.register_function(a, 'a') >>> server.serve_forever() 127.0.0.1 - - [22/Apr/2014 22:37:27] "POST / HTTP/1.1" 200 - 127.0.0.1 - - [22/Apr/2014 22:37:39] "POST / HTTP/1.1" 200 - 127.0.0.1 - - [22/Apr/2014 22:38:04] "POST / HTTP/1.1" 200 - |
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 | Python 2.7.5 (default, May 15 2013, 22:43:36) [MSC v.1500 32 bit (Intel)] on win32 Type "copyright", "credits" or "license()" for more information. >>> import xmlrpclib >>> proxy = xmlrpclib.ServerProxy("http://localhost:8000/") >>> proxy.a() Traceback (most recent call last): File "<pyshell#2>", line 1, in <module> proxy.a() File "C:\Python27\lib\xmlrpclib.py", line 1224, in __call__ return self.__send(self.__name, args) File "C:\Python27\lib\xmlrpclib.py", line 1578, in __request verbose=self.__verbose File "C:\Python27\lib\xmlrpclib.py", line 1264, in request return self.single_request(host, handler, request_body, verbose) File "C:\Python27\lib\xmlrpclib.py", line 1297, in single_request return self.parse_response(response) File "C:\Python27\lib\xmlrpclib.py", line 1473, in parse_response return u.close() File "C:\Python27\lib\xmlrpclib.py", line 793, in close raise Fault(**self._stack[0]) Fault: <Fault 1: "<type 'exceptions.TypeError'>:must be string or buffer, not dict"> >>> proxy.a() <xmlrpclib.Binary instance at 0x02AEC508> >>> proxy.a().data '{"a": 1}' >>> import json >>> json.loads(proxy.a().data) {u'a': 1} >>> |
另一种注册方法:
http://soft.zdnet.com.cn/software_zone/2008/0527/887034.shtml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | import SimpleXMLRPCServer #定义自己的CMS类 class MyCMS: def getVersion(self):#向外公开版本的方法 return "Powerd By python 0.1a" cms = MyCMS() server = SimpleXMLRPCServer.SimpleXMLRPCServer(("localhost", 8888)) server.register_instance(cms) print "Listening on port 8888" server.serve_forever()#服务器执行,并监听8888端口 import xmlrpclib server = xmlrpclib.ServerProxy("http://localhost:8888";) version = server.getVersion() print "version:"+version |
参考:
https://docs.python.org/2/library/xmlrpclib.html#fault-objects