感谢您的回答。
我已经阅读了这个文档,但不幸的是方法 read-into_file 和 make_file, read ...它对我不起作用。例如,当尝试读取从我的 java 客户端发送的 zip 文件时:
假设data是Http post参数
make_file()
fp = data.make_file()
print("fp type", type(fp)) # _io.BufferedRandom
zipFile = fp.read()
输出:
AttributeError: 'bytes' object has no attribute 'seek'
第 651 行,在 read_lines_to_boundary 中引发 EOFError("Illegal end of multipart body.")EOFError: Illegal end of multipart body.
read_into_file()
file = data.read_into_file()
print("file type", type(file))
zipFile = io.BytesIO(file.read())
# zipFile = file.read() # => raises same error
输出:
line 651, in read_lines_to_boundary raise EOFError("Illegal end of multipart body.")EOFError: Illegal end of multipart body.
我不明白会发生什么......
实际上“数据”不是像对象这样的文件,而是cherrypy._cpreqbody.Part 1。它包含一个“文件”文件和一个 _io.BufferedRandom 类属性。
它的 read() 方法以二进制形式(字节)返回整个正文内容。
所以最简单的解决方案是:
class BinReceiver(object):
def index(self, data):
zipFile = io.BytesIO(data.file.read())
path = "/tmp/data.zip"
fp = open(path)
fp.write(zipFile, 'wb')
fp.close()
print("saved data into", path, "size", len(zipFile))
index.exposed = True
这很好用......
仅供参考:我正在运行 python3.2