Connecting Tech Pros Worldwide Help | Site Map

XMLRPC binary file transfer

Newbie
 
Join Date: Mar 2007
Posts: 15
#1: Jan 14 '09
Hi all,

I am trying to transfer files via xmlrpc. I am following the example found here:
http://docs.python.org/library/xmlrpclib.html
under the binary objects section

Server Code
Expand|Select|Wrap|Line Numbers
  1. from SimpleXMLRPCServer import SimpleXMLRPCServer
  2. import xmlrpclib
  3.  
  4. def python_logo():
  5.      handle = open("python_logo.jpg")
  6.      return xmlrpclib.Binary(handle.read())
  7.      handle.close()
  8.  
  9. server = SimpleXMLRPCServer(("localhost", 8000))
  10. print "Listening on port 8000..."
  11. server.register_function(python_logo, 'python_logo')
  12.  
  13. server.serve_forever()[FONT=Arial][/FONT]
Client Code
Expand|Select|Wrap|Line Numbers
  1. import xmlrpclib
  2.  
  3. proxy = xmlrpclib.ServerProxy("http://localhost:8000/")
  4. handle = open("fetched_python_logo.jpg", "w")
  5. handle.write(proxy.python_logo().data)
  6. handle.close()
I can get txt files to transfer properly, but when I try to transfer other things like word documents and images it creates a file that are a fraction of the original size and nothing shows up when I open the file.

Am I missing something in my Python install that converts the binary objects properly?

Thanks.
kudos's Avatar
Expert
 
Join Date: Jul 2006
Location: Norway
Posts: 111
#2: Jan 15 '09

re: XMLRPC binary file transfer


Try the following first, change :

Expand|Select|Wrap|Line Numbers
  1. handle = open("fetched_python_logo.jpg", "w")
to

Expand|Select|Wrap|Line Numbers
  1. handle = open("fetched_python_logo.jpg", "wb") 
(It could be something else aswell, but try this first)

-kudos
Newbie
 
Join Date: Mar 2007
Posts: 15
#3: Jan 15 '09

re: XMLRPC binary file transfer


I added the "b" into the code but I am still having the same problem. I tried the code on linux and mac and it worked fine, but when I tried it on another windows machine(I am also running on windows) the same problem occurred. I guess python on windows does not play with this binary transfer nicely?
Newbie
 
Join Date: May 2009
Posts: 5
#4: May 20 '09

re: XMLRPC binary file transfer


On server side, you must do something like this:

Expand|Select|Wrap|Line Numbers
  1. handle = open("python_logo.jpg", 'rb')
Also, on client side, like @kudos said, you can write 'wb' instead 'w', but this is not mandatory...

Regards,
Stole
Reply