Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Python将大型zip文件上载到网站_Python_Python 2.7 - Fatal编程技术网

使用Python将大型zip文件上载到网站

使用Python将大型zip文件上载到网站,python,python-2.7,Python,Python 2.7,我有以下问题:我需要将大型.zip文件(通常大于500MB,最大为ca 5GB)上传到网站,然后由网站处理这些文件。我在Windows32位上的Python 2.7.16中实现了这一点。遗憾的是,由于公司限制,我无法更改设置(从32位更改为64位),也无法安装其他Python插件(我有请求、urllib和urllib2以及其他几个已安装)。我的代码现在看起来像这样: import requests FileList=["C:\File01.zip", "C:\

我有以下问题:我需要将大型.zip文件(通常大于500MB,最大为ca 5GB)上传到网站,然后由网站处理这些文件。我在Windows32位上的Python 2.7.16中实现了这一点。遗憾的是,由于公司限制,我无法更改设置(从32位更改为64位),也无法安装其他Python插件(我有请求、urllib和urllib2以及其他几个已安装)。我的代码现在看起来像这样:

 import requests

 FileList=["C:\File01.zip", "C:\FileA02.zip", "C:\UserFile993.zip"]
 UploadURL = "https://mywebsite.com/submitFile"
 for FilePath in FileList:
    print("Upload file: "+str(FilePath))
    session = requests.Session()
        with open(FilePath, "rb") as file:
        session.post(UploadURL,data={'file':'Send file'},files={'FileToBeUploaded':FilePath})
    print("Upload done: "+str(FilePath))
    session.close()
由于我的
文件列表
相当长(>100个条目),我只是在这里粘贴了它的一个摘录。如果存在低于600MB的文件,则上述代码可以正常工作。上面的任何文件都会引发此错误:

  File "<stdin>", line 1, in <module>
  File "C:\Users\AAA253\Desktop\DingDong.py", line 39, in <module>
    session.post(UploadURL,data={'file':'Send file'},files={'FileToBeUploaded':FilePath})
  File "C:\Python27\lib\site-packages\requests\sessions.py", line 522, in post
    return self.request('POST', url, data=data, json=json, **kwargs)
  File "C:\Python27\lib\site-packages\requests\sessions.py", line 461, in request
    prep = self.prepare_request(req)
  File "C:\Python27\lib\site-packages\requests\sessions.py", line 394, in prepare_request
    hooks=merge_hooks(request.hooks, self.hooks),
  File "C:\Python27\lib\site-packages\requests\models.py", line 297, in prepare
    self.prepare_body(data, files, json)
  File "C:\Python27\lib\site-packages\requests\models.py", line 455, in prepare_body
    (body, content_type) = self._encode_files(files, data)
  File "C:\Python27\lib\site-packages\requests\models.py", line 158, in _encode_files
    body, content_type = encode_multipart_formdata(new_fields)
  File "C:\Python27\lib\site-packages\requests\packages\urllib3\filepost.py", line 86, in encode_multipart_formdata
    body.write(data)
MemoryError
然而,我得到了几乎相同的错误:

    File "<stdin>", line 1, in <module>
  File "C:\Users\AAA253\Desktop\DingDong.py", line 48, in <module>
    resp =  session.post(UploadURL,data={'file':'Send file'},files={'FileToBeUploaded':form})
  File "C:\Python27\lib\site-packages\requests-2.24.0-py2.7.egg\requests\sessions.py", line 578, in post
    return self.request('POST', url, data=data, json=json, **kwargs)
  File "C:\Python27\lib\site-packages\requests-2.24.0-py2.7.egg\requests\sessions.py", line 516, in request
    prep = self.prepare_request(req)
  File "C:\Python27\lib\site-packages\requests-2.24.0-py2.7.egg\requests\sessions.py", line 459, in prepare_request
    hooks=merge_hooks(request.hooks, self.hooks),
  File "C:\Python27\lib\site-packages\requests-2.24.0-py2.7.egg\requests\models.py", line 317, in prepare
    self.prepare_body(data, files, json)
  File "C:\Python27\lib\site-packages\requests-2.24.0-py2.7.egg\requests\models.py", line 505, in prepare_body
    (body, content_type) = self._encode_files(files, data)
  File "C:\Python27\lib\site-packages\requests-2.24.0-py2.7.egg\requests\models.py", line 159, in _encode_files
    fdata = fp.read()
  File "build\bdist.win32\egg\requests_toolbelt\multipart\encoder.py", line 314, in read
  File "build\bdist.win32\egg\requests_toolbelt\multipart\encoder.py", line 194, in _load
  File "build\bdist.win32\egg\requests_toolbelt\multipart\encoder.py", line 256, in _write
  File "build\bdist.win32\egg\requests_toolbelt\multipart\encoder.py", line 552, in append
MemoryError
文件“”,第1行,在
文件“C:\Users\AAA253\Desktop\DingDong.py”,第48行,在
resp=session.post(上传URL,数据={'file':'Send file'},文件={'FileToBeUploaded':form})
文件“C:\Python27\lib\site packages\requests-2.24.0-py2.7.egg\requests\sessions.py”,第578行,在post中
return self.request('POST',url,data=data,json=json,**kwargs)
文件“C:\Python27\lib\site packages\requests-2.24.0-py2.7.egg\requests\sessions.py”,第516行,请求中
准备=自我准备请求(req)
文件“C:\Python27\lib\site packages\requests-2.24.0-py2.7.egg\requests\sessions.py”,第459行,在prepare\u请求中
钩子=合并钩子(request.hooks,self.hooks),
文件“C:\Python27\lib\site packages\requests-2.24.0-py2.7.egg\requests\models.py”,第317行,在prepare中
self.prepare_body(数据、文件、json)
文件“C:\Python27\lib\site packages\requests-2.24.0-py2.7.egg\requests\models.py”,第505行,在prepare\u body中
(正文,内容类型)=自编码文件(文件,数据)
文件“C:\Python27\lib\site packages\requests-2.24.0-py2.7.egg\requests\models.py”,第159行,在编码文件中
fdata=fp.read()
文件“build\bdist.win32\egg\requests\u toolbelt\multipart\encoder.py”,第314行,已读
文件“build\bdist.win32\egg\requests\u toolbelt\multipart\encoder.py”,第194行,装入
文件“build\bdist.win32\egg\requests\u toolbelt\multipart\encoder.py”,第256行,写入
文件“build\bdist.win32\egg\requests\u toolbelt\multipart\encoder.py”,第552行,在append中
记忆者

您很可能需要流式多端口编码器


即使你的公司限制禁止安装新软件包,你也可以将你需要的部分(可能是整个软件包)放到你的项目目录中。

你检查过你的目标站点是否接受分块吗?我现在就实现了这一点,并扩展了我的第一篇文章。我仍然会出错。您需要使用
data=form
,而不是
files={…:form}
。好吧,使用您的想法我仍然无法完成。我没有看到代码和我的网站之间的交互。在我的网站上,我有两个按钮发送文件。名为“发送文件”的按钮启动上载,而在名为“FileToBeUploaded”的第二个按钮上,您需要选择要上载的文件。我使用multipartincoder做这件事是不是完全错了:
m=multipartincoder(fields={'file':'Send file','FileToBeUploaded':('filename',open(FilePath,'rb'),'.zip')})r=session.post(UploadURL,data=m,headers={'Content-Type':m.Content\u-Type})
但是你没有用这个版本得到memoryror,正确的?然后就要发送正确的数据了。MemoryError消失了,但服务器没有回复,所以我担心我没有收到错误,而是发送没有激活(加载到内存中也是如此)。
    File "<stdin>", line 1, in <module>
  File "C:\Users\AAA253\Desktop\DingDong.py", line 48, in <module>
    resp =  session.post(UploadURL,data={'file':'Send file'},files={'FileToBeUploaded':form})
  File "C:\Python27\lib\site-packages\requests-2.24.0-py2.7.egg\requests\sessions.py", line 578, in post
    return self.request('POST', url, data=data, json=json, **kwargs)
  File "C:\Python27\lib\site-packages\requests-2.24.0-py2.7.egg\requests\sessions.py", line 516, in request
    prep = self.prepare_request(req)
  File "C:\Python27\lib\site-packages\requests-2.24.0-py2.7.egg\requests\sessions.py", line 459, in prepare_request
    hooks=merge_hooks(request.hooks, self.hooks),
  File "C:\Python27\lib\site-packages\requests-2.24.0-py2.7.egg\requests\models.py", line 317, in prepare
    self.prepare_body(data, files, json)
  File "C:\Python27\lib\site-packages\requests-2.24.0-py2.7.egg\requests\models.py", line 505, in prepare_body
    (body, content_type) = self._encode_files(files, data)
  File "C:\Python27\lib\site-packages\requests-2.24.0-py2.7.egg\requests\models.py", line 159, in _encode_files
    fdata = fp.read()
  File "build\bdist.win32\egg\requests_toolbelt\multipart\encoder.py", line 314, in read
  File "build\bdist.win32\egg\requests_toolbelt\multipart\encoder.py", line 194, in _load
  File "build\bdist.win32\egg\requests_toolbelt\multipart\encoder.py", line 256, in _write
  File "build\bdist.win32\egg\requests_toolbelt\multipart\encoder.py", line 552, in append
MemoryError