Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send file from client to server using XMLRPC?

I want to write Python code to send a file from client to server. server needs to save the file sent from the client. But my code have some bugs which I cannot fix. Below is my server code:

# server.py
from SimpleXMLRPCServer import SimpleXMLRPCServer
import os

server = SimpleXMLRPCServer(('localhost', 9000))

def save_data(data):
    handle = open("x123.dat", "wb")
    handle.write(data)
    handle.close()

server.register_function(save_data, 'save_data')
server.serve_forever()

And the client code:

# client.py
import sys, xmlrpclib

proxy = xmlrpclib.Server('http://localhost:9000')
handle = open(sys.argv[1], "rb")
proxy.save_data(handle.read())
handle.close()

But then I run my code, the client returns the following error (this is on Windows):

Traceback (most recent call last):
File "client.py", line 6, in <module> proxy.save_data(handle.read())
File "c:\python27\lib\xmlrpclib.py", line 1224, in __call__
  return self.__send(self.__name, args)
File "c:\python27\lib\xmlrpclib.py", line 1575, 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])
xmlrpclib.Fault: <Fault 1: "<class 'xml.parsers.expat.ExpatError'>:not well-formed (invalid token): line 7, column 1">

I have some questions:

  1. How to fix the above bug?

  2. My code needs to transfer some big files sometimes. Since my method is so simple, I doubt that it is efficient for moving big data. Could anybody please suggest a better method to move big files? (Of course it is better to use XMLRPC on Python)

like image 310
user311703 Avatar asked Feb 01 '12 16:02

user311703


People also ask

What is XML-RPC client?

XML-RPC is a Remote Procedure Call method that uses XML passed via HTTP(S) as a transport. With it, a client can call methods with parameters on a remote server (the server is named by a URI) and get back structured data.

How do I enable Allow_none on XML-RPC?

In your server class, add allow_none=True to your SimpleXMLRPCServer instantiation. The allow_none and encoding parameters are passed on to xmlrpc. client and control the XML-RPC responses that will be returned from the server.

How request is sent in XML-RPC?

Requests are encoded in XML and sent via HTTP POST. XML responses are embedded in the body of the HTTP response. Because XML-RPC is platform-independent, it allows diverse applications to communicate. For example, a Java client can speak XML-RPC to a Perl server.

Which of the below method does XML-RPC uses to send the request?

XML-RPC uses the HTTP protocol to pass information from a client computer to a server computer.


2 Answers

Server side:

def server_receive_file(self,arg):
        with open("path/to/save/filename", "wb") as handle:
            handle.write(arg.data)
            return True

Client side:

with open("path/to/filename", "rb") as handle:
    binary_data = xmlrpclib.Binary(handle.read())
client.server_receive_file(binary_data)

This worked for me.

like image 198
Kiran6699 Avatar answered Oct 11 '22 12:10

Kiran6699


You want to look into the xmlrpclib Binary object. With this class you can encode and decode to/from a base64 string.

like image 45
John Giotta Avatar answered Oct 11 '22 14:10

John Giotta