【发布时间】:2014-12-21 23:16:28
【问题描述】:
我想将一个大文件从 python 客户端发布到cherrypy。我正在使用请求库。
这是我的客户端代码:
def upload(fileName=None):
url = 'http://localhost:8080/upload'
files = {'myFile': ( fileName, open(fileName, 'rb') )}
r = requests.post(url, files=files)
#with open(fileName,'rb') as payload:
#headers = {'content-type': 'multipart/form-data'}
#r = requests.post('http://127.0.0.1:8080', data=payload,verify=False,headers=headers)
if __name__ == '__main__':
upload(sys.argv[1])
问题在于这会将整个文件放入 RAM 内存中。有什么办法可以分片发布文件吗?
class FileDemo(object):
@cherrypy.expose
def upload(self, myFile):
print myFile.filename
#size = 0
#decoder = MultipartDecoder(myFile, 'image/jpeg')
#for part in decoder.parts:
#print(part.header['content-type'])
#while True:
#advances to the content that hasn't been read
#myFile.file.seek(size, 0)
#reads 100mb at a time so it doesn't fill up the RAM
#data = myFile.file.read(10240000)
#newFile = open("/home/ivo/Desktop/"+str(myFile.filename), 'a+')
#newFile.write(data)
#newFile.close
#size += len(data)
#if len(data) < 10240000:
#break
if __name__ == '__main__':
cherrypy.quickstart(FileDemo())
这是服务器端的代码。它有很多 cmets,因为我一直在尝试很多东西。现在我只是打印文件名,客户端仍然将整个文件传输到 RAM。
我不知道还能尝试什么。提前感谢您的帮助。
【问题讨论】:
-
首先,它与 CherryPy 无关。其次,请在提问之前搜索您的问题。这是the same question 的答案。
-
如果我问这个问题是因为我尝试了很多不同的东西,包括那个答案中的代码,但它对我不起作用。还是谢谢你
-
对不起。我的第一条评论无关紧要。事实证明,这个话题比我最初想象的要广泛,因为“多部分”编码很难应用于
mmap'ed 文件。此外,mmaped文件在 32 位操作系统上存在 ~2GiB 限制的可移植性问题。
标签: python file post cherrypy multipart