Python 由于UnicodeDecodeError,Twisted FileSender无法发送二进制文件
在Twisted中尝试通过HTTP发送文件时,我遇到了一个相当恼人的问题。我对Python相当陌生,因为这是我第一次真正尝试做除Hello World等之外的任何事情 现在,我相当确定当FileSender发送不兼容UTF-8的二进制数据时会出现问题。在网络上移动图像、视频、可执行二进制文件等文件时,这似乎是一件相当常见的事情 在我看来,当初始化时,Twisted缓冲区被初始化为,这里我可能非常错误地认为它是UTF-8,或者Python的默认字符编码是字符串?。。。然后,当新数据添加到缓冲区时,旧缓冲区UTF-8与新缓冲区er连接。。。其他的二进制编码?其结果是:Python 由于UnicodeDecodeError,Twisted FileSender无法发送二进制文件,python,utf-8,binary,twisted,Python,Utf 8,Binary,Twisted,在Twisted中尝试通过HTTP发送文件时,我遇到了一个相当恼人的问题。我对Python相当陌生,因为这是我第一次真正尝试做除Hello World等之外的任何事情 现在,我相当确定当FileSender发送不兼容UTF-8的二进制数据时会出现问题。在网络上移动图像、视频、可执行二进制文件等文件时,这似乎是一件相当常见的事情 在我看来,当初始化时,Twisted缓冲区被初始化为,这里我可能非常错误地认为它是UTF-8,或者Python的默认字符编码是字符串?。。。然后,当新数据添加到缓冲区时,
Unhandled Error
Traceback (most recent call last):
Failure: exceptions.RuntimeError: Producer was not unregistered for /vfs/videos/thevid.avi
Unhandled Error
Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/twisted/python/log.py", line 84, in callWithLogger
return callWithContext({"system": lp}, func, *args, **kw)
File "/usr/lib/python2.7/dist-packages/twisted/python/log.py", line 69, in callWithContext
return context.call({ILogContext: newCtx}, func, *args, **kw)
File "/usr/lib/python2.7/dist-packages/twisted/python/context.py", line 118, in callWithContext
return self.currentContext().callWithContext(ctx, func, *args, **kw)
File "/usr/lib/python2.7/dist-packages/twisted/python/context.py", line 81, in callWithContext
return func(*args,**kw)
--- <exception caught here> ---
File "/usr/lib/python2.7/dist-packages/twisted/internet/selectreactor.py", line 146, in _doReadOrWrite
why = getattr(selectable, method)()
File "/usr/lib/python2.7/dist-packages/twisted/internet/tcp.py", line 428, in doWrite
result = abstract.FileDescriptor.doWrite(self)
File "/usr/lib/python2.7/dist-packages/twisted/internet/abstract.py", line 199, in doWrite
self.dataBuffer = buffer(self.dataBuffer, self.offset) + "".join(self._tempDataBuffer)
File "/usr/lib/python2.7/encodings/utf_8.py", line 16, in decode
return codecs.utf_8_decode(input, errors, True)
exceptions.UnicodeDecodeError: 'utf8' codec can't decode byte 0xd3 in position 6: invalid continuation byte
Unhandled Error
Traceback (most recent call last):
Failure: exceptions.RuntimeError: Producer was not unregistered for /vfs/videos/thevid.avi
请附上一个更完整的示例,理想情况下,您可以独立运行,即连接到自身以便我可以看到错误,或者至少可以肯定地说我没有看到它。对于它的价值,扭曲不考虑通过文本写入或写入等式为UTF-8;它们只是字节,总是字节。事实上,如果您尝试以unicode方式传递它们,它们将引发类型错误。我不知道unicode对象是如何在_tempBuffer中结束的;这应该是不可能的。所以我不能说得更多,除非我能确切地看到是什么代码导致了这种情况……除了在很长一段时间内,writeSequence不会检查,所以你可能会忽略它。Twisted的最新版本将在这两个地方进行检查,因此可能更容易找到此错误的来源。
def writeFile(self, request, location):
# Setup headers (mime type, filename)
request.setHeader('Content-Type', mimetypes.guess_type("file://" + location))
request.setHeader('Content-Disposition', "attachment; filename=" + path.basename(location))
# Open file
handle = open(location, "rb")
# Setup transfer (then cleanup etc)
d = FileSender().beginFileTransfer(handle, request)
def fileFinished(ignored):
handle.close()
request.finish()
d.addCallback(fileFinished).addErrback(fileFinished)