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...
import xmlrpclibserver = xmlrpclib.ServerProxy('http://127.0.0.1:9009')filepath=raw_input('Enter path to file :')
with open(filepath, "rb") as handle:
print "Upload failed"
ftp server :
import xmlrpclibfrom SimpleXMLRPCServer import SimpleXMLRPCServerdef UploadFile(filedata):
with open(fp, "wb") as handle:
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 xmlrpclibfrom SimpleXMLRPCServer import SimpleXMLRPCServer
handle = open(filepath)
ftp client :
server = xmlrpclib.ServerProxy('http://127.0.0.1:9009')filepath=raw_input('enter path : ')
print 'Download failed'
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.