So I'm trying to learn Pyro by creating a simple file server with it. I've implemented a few basic features, and now I'm trying to add file transfer. I've looked at examples of how to do this (https://github.com/irmen/Pyro4/tree/master/examples/filetransfer), and the way it seems to be with pure Pyro done is just returning the bytes read from the file and writing them on the receiving end.
This is what I've done (I know I really should break up the files when sending them, but I can do that once this issue is fixed):
client.py
import Pyro4
server= Pyro4.Proxy("PYRONAME:server")
def download(file_name):
output_file = open(file_name, "wb")
output_file.write(server.download(file_name))
output_file.close()
print "Downloaded file: {}".format(file_name)
server.py
import Pyro4
@Pyro4.expose
class Server(object):
def download(self, file_name):
return open(file_name, "rb").read()
daemon = Pyro4.Daemon()
ns = Pyro4.locateNS()
uri = daemon.register(Server)
ns.register("server", uri)
daemon.requestLoop()
This works just fine for simple files like some .txt documents, but when I try other file types, for example .pdf, I get the error:
UnicodeEncodeError: 'ascii' codec can't encode characters in position 11-14: ordinal no in range(128)
I've spent some time looking up this error, and the closest I can come to a solution is by changing this line in client.py:
output_file.write(server.download(file_name))
to:
output_file.write(server.download(file_name).encode("ascii", "replace"))
This avoids the error, completes the download, and gives a file of the right size. But the file becomes corrupted and unopenable.
Any suggestions how to fix this? If not is there any other way to implement file transfer with Pyro?