01 February, 2011

a simple ftp program with python xmlrpc

Hope you have read my post about creating a simple client server program with python xmlrpc. Now we will  try creating a sample file transfer program through which we can transfer files (text, images, movie) over the network. The logic behind this is the same that we used in the client server program shown in the above link. The remote procedure call allows to call functions placed in a remote server, passing parameters to it and receiving return values from the remote function in the server. 

1. Sending a file to remote server 

       Inorder to send a file, say a picture file we first open the file and read it's contents. Then we call the remote function in the server with file content (of the picture) as parameter. The remote function on receiving this contents as parameters will open a new file and write it into that file...

Check the ftp client and server program below...

ftp client


   import xmlrpclib
   server = xmlrpclib.ServerProxy('http://127.0.0.1:9009')
   filepath=raw_input('Enter path to file :')
   try:
         with open(filepath, "rb") as handle:
            data=xmlrpclib.Binary(handle.read())
            handle.close()
            a=server.UploadFile(data
   except:
         print "Upload failed"



 ftp server :
 

   import xmlrpclib
   from SimpleXMLRPCServer import SimpleXMLRPCServer 
   def UploadFile(filedata):
        try:
            fp="/home/jo/Downloads/file2"
            with open(fp, "wb") as handle:
                    data1=filedata.data
                    handle.write(data1)
                    handle.close()
            return True
        except Exception,ex:
            return 'error'
   client.register_function(UploadFile)
   server.serve_forever()


2. Receiving a file from remote server 

         Here we call the remote function on the server with file path as the parameter, where file path is the path to the file that is going to be downloaded to the client. Server on receiving the file path, opens file and returns it's contents to the client which called the remote function. Client will open a new file and writes the  above returned value to the file.

ftp server :


     import xmlrpclib
    from SimpleXMLRPCServer import SimpleXMLRPCServer
    def movefile(filepath):
        try:
            handle = open(filepath)
            return xmlrpclib.Binary(handle.read())
            handle.close()
        except:
            return 'error'
    client.register_function(movefile)
    server.serve_forever()


ftp client :

   import xmlrpclib
   server = xmlrpclib.ServerProxy('http://127.0.0.1:9009')
   filepath=raw_input('enter path : ')
   try:
         handle=open("/home/jo/Downloads/file1","w")
         handle.write(server.movefile(filepath).data)
         handle.close()
    except:
         print 'Download failed'



remarks ::

In the above examples the data read from the file is converted to binary format before transferring it using the Binary() function in the xmlrpc library. Also you have to change the ip to desired on replacing 127.0.0.1 to run the program on a network.        


Read rest of entry
 

Terminal Diary | techblog Copyright © 2009 Gadget Blog is Designed by jintu jacob Powered by Blogger