简体   繁体   English

使用Python 2.7 / Pyro4 UnicodeEncodeError传输文件

[英]Transfer files with Python 2.7/Pyro4 UnicodeEncodeError

So I'm trying to learn Pyro by creating a simple file server with it. 因此,我试图通过使用它创建一个简单的文件服务器来学习Pyro。 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. 我已经看过如何执行此操作的示例( https://github.com/irmen/Pyro4/tree/master/examples/filetransfer ),而使用纯Pyro的方式似乎只是返回读取的字节从文件中写入到接收端。

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 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 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: 这对于一些简单的文件(例如某些.txt文档)来说很好用,但是当我尝试其他文件类型(例如.pdf)时,出现错误:

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: 我花了一些时间查找此错误,而最接近解决方案的方法是更改​​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? 如果没有其他方法可以用Pyro进行文件传输?

This is because file.write can write only bytes and it also tries to convert input string to ascii encoding. 这是因为file.write只能写入字节,并且还会尝试将输入字符串转换为ascii编码。 Your pdf file is of a different encoding (Maybe UTF-8). 您的pdf文件使用不同的编码(也许是UTF-8)。 You can try something like 您可以尝试类似

fptr.write(byte_string).encode("utf-8"))

I observed same error while working with files. 在处理文件时,我观察到相同的错误。 And this was due to some encoding other than ascii. 这是由于除了ascii之外的一些编码。 You may refer to this SO Post for more info . 您可以参考此SO Post以获得更多信息。 For more on pdf encoding you may refer to page 86 of this pdf. 有关pdf编码的更多信息,请参阅 pdf的第86页。

see here: https://github.com/irmen/Pyro4/blob/master/examples/filetransfer/client.py#L14 看到这里: https : //github.com/irmen/Pyro4/blob/master/examples/filetransfer/client.py#L14

If using serpent, you'll need to pay special attention to properly processing the serialized bytes. 如果使用蛇,则需要特别注意正确处理序列化的字节。 The example shows how to do this. 该示例显示了如何执行此操作。

More efficient wire encoding of binary data is done when using the pickle or marshal serializers. 使用pickle或marshal序列化程序时,将对二进制数据进行更有效的有线编码。 Details here https://pyro4.readthedocs.io/en/stable/tipstricks.html#binary-data-transfer-file-transfer 此处的详情https://pyro4.readthedocs.io/en/stable/tipstricks.html#binary-data-transfer-file-transfer

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM