XMLRPC binary file transfer

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • celery6541
    New Member
    • Mar 2007
    • 15

    XMLRPC binary file transfer

    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
    Code:
    from SimpleXMLRPCServer import SimpleXMLRPCServer
    import xmlrpclib
    
    def python_logo():
         handle = open("python_logo.jpg")
         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()[FONT=Arial][/FONT]
    Client Code
    Code:
    import xmlrpclib
    
    proxy = xmlrpclib.ServerProxy("http://localhost:8000/")
    handle = open("fetched_python_logo.jpg", "w")
    handle.write(proxy.python_logo().data)
    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
    Recognized Expert New Member
    • Jul 2006
    • 127

    #2
    Try the following first, change :

    Code:
    handle = open("fetched_python_logo.jpg", "w")
    to

    Code:
    handle = open("fetched_python_logo.jpg", "wb")
    (It could be something else aswell, but try this first)

    -kudos

    Comment

    • celery6541
      New Member
      • Mar 2007
      • 15

      #3
      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?

      Comment

      • Condor06
        New Member
        • May 2009
        • 7

        #4
        On server side, you must do something like this:

        Code:
        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

        Comment

        Working...