栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何上传超过2147483647字节的字符串块?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何上传超过2147483647字节的字符串块?

requests
bug跟踪器上询问了您的问题;
他们的建议是使用流式上传。如果这不起作用,您可能会看到块编码的请求是否有效。

[编辑]

基于原始代码的示例:

# Using `with` here will handle closing the file implicitlywith open(attachment_path, 'rb') as file_to_upload:    r = requests.put(        "{base}problems/{pid}/{atype}/{path}".format( base=self._baseurl, # It's better to use consistent naming; search PEP-8 for standard Python conventions. pid=problem_id, atype=attachment_type, path=urllib.quote(os.path.basename(attachment_path)),        ),        headers=headers,        # Note that you're passing the file object, NOT the contents of the file:        data=file_to_upload,        # Hard to say whether this is a good idea with a large file upload        timeout=300,    )

我不能保证它会按原样运行,因为我无法实际测试它,但是它应该很接近。我链接到的错误跟踪器注释还提到,发送多个标头可能会导致问题,因此,如果实际上指定的标头是必需的,则此方法可能无效。

关于块编码:这应该是您的第二选择。您的代码未指定

'rb'
为的模式
open(...)
,因此更改该模式可能应使上述代码正常工作。如果没有,您可以尝试一下。

def read_in_chunks():    # If you're going to chunk anyway, doesn't it seem like smaller ones than this would be a good idea?    chunk_size = 30720 * 30720    # I don't know how correct this is; if it doesn't work as expected, you'll need to debug    with open(attachment_path, 'rb') as file_object:        while True: data = file_object.read(chunk_size) if not data:     break yield data# Same request as above, just using the function to chunk explicitly; see the `data` paramr = requests.put(    "{base}problems/{pid}/{atype}/{path}".format(        base=self._baseurl,        pid=problem_id,        atype=attachment_type,        path=urllib.quote(os.path.basename(attachment_path)),    ),    headers=headers,    # Call the chunk function here and the request will be chunked as you specify    data=read_in_chunks(),    timeout=300,)


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/646332.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号