python xmlrpc实现二进制文件传输的代码,简单的远程调用

把服务器端文件读取到本地


服务器端

view source
print ?
from SimpleXMLRPCServer import SimpleXMLRPCServer
import xmlrpclib
  
def python_logo():
     handle = open("python_logo.jpg",'rb')
     return xmlrpclib.Binary(handle.read())
     handle.close()
  
server = SimpleXMLRPCServer(("localhost"8000))
print "Listening on port 8000..."
server.register_function(python_logo, 'python_logo')
  
server.serve_forever()
 
 
 
#该代码片段来自于: http://www.sharejs.com/codes/python/5725
客户端代码
import xmlrpclib
  
proxy = xmlrpclib.ServerProxy("http://localhost:8000/")
handle = open("fetched_python_logo.jpg""wb")
handle.write(proxy.python_logo().data)
handle.close()

通过XML-RPC 实现简单的远程调用,你想找到一个简单的方式去执行运行在远程机器上面的Python 程序中的函数或方

#from xmlrpc.server import SimpleXMLRPCServer

from SimpleXMLRPCServer import SimpleXMLRPCServer
import xmlrpclib
from xmlrpclib import ServerProxy


class KeyValueServer:
    _rpc_methods_ = ['get', 'set', 'delete', 'exists', 'keys']
    def __init__(self, address):
        self._data = {}
        self._serv = SimpleXMLRPCServer(address, allow_none=True)
        for name in self._rpc_methods_:
            self._serv.register_function(getattr(self, name))


    def get(self, name):
        return self._data[name]

    def set(self, name, value):
        self._data[name] = value

    def delete(self, name):
        del self._data[name]

    def exists(self, name):
        return name in self._data

    def keys(self):
        return list(self._data)

    def serve_forever(self):
        self._serv.serve_forever()


# Example
if __name__ == '__main__':
    kvserv = KeyValueServer(('', 15000))
    print "xml-rpc running."
    kvserv.serve_forever()



client:

>>> from xmlrpclib import ServerProxy
>>> s = ServerProxy('http://localhost:15000', allow_none=True)
>>> s.set('foo', 'bar')
>>> s.set('spam', [1, 2, 3])
>>> s.keys()
['spam', 'foo']
>>> s.get('foo')
'bar'
>>> s.get('spam')
[1, 2, 3]
>>> s.delete('spam')
>>> s.exists('spam')
False
>>>


你可能感兴趣的:(python xmlrpc实现二进制文件传输的代码,简单的远程调用)