我有以下脚本允许我将文件上传到用户文件。它不适用于大文件(例如1GB)。可能会有什么变化?
import requests
import random
import re
filehandle = open("menu.avi", "rb")
resp=requests.get("https://usersfiles.com/")
sess_id = re.search('sess_id.*=?"(.*)?"', str(resp.text)).group(1)
srv_tmp_url = re.search('srv_tmp_url.*=?"(.*)?"', str(resp.text)).group(1)
upload_type = re.search('upload_type.*=?"(.*)?"', str(resp.text)).group(1)
UID = ''
for i in range(0, 12):
UID = UID + '' + str(random.randint(0,10))
url2="https://up11.usersfiles.com/cgi-bin/upload.cgi?upload_id="+UID+"&js_on=1&utype=reg&upload_type="+upload_type
r = requests.post(url2, data={"upload_type":upload_type , "sess_id":sess_id,
"srv_tmp_url":srv_tmp_url}, files = {"file_0":filehandle})
link_usersfiles = re.search('name=.fn.>(.*?)<', str(r.text)).group(1)
此脚本生成错误:
body.write(数据)
的MemoryError
答案 0 :(得分:0)
默认情况下,在上传文件时, requests
会将整个文件读入内存,因此在上传大文件时可能会耗尽。最简单的方法是安装 requests-toolbelt
,这可以轻松地上传文件。
对于您的示例,您可以使用以下内容:
import requests
from requests_toolbelt.multipart.encoder import MultipartEncoder
# ... code for preparing for upload ...
m = MultipartEncoder(
fields={'upload_type': upload_type, 'sess_id': sess_id,
'file_0': ('filename', file handle, 'text/plain')}
)
r = requests.post(url2, data=m,
headers={'Content-Type': m.content_type})
有关详细信息,请参阅https://toolbelt.readthedocs.org/en/latest/uploading-data.html