【问题标题】:Creating ZIP file in Memory and sending via POST request在内存中创建 ZIP 文件并通过 POST 请求发送
【发布时间】:2019-10-23 06:23:32
【问题描述】:

我正在尝试使用 python 在内存中创建一个 zip 文件,然后我将其附加到 POST 请求以使用 python-requests 发送。这是我写的函数

import StringIO,zipfile
code = "poopootest"
def _build_zip_inmem(code):
    mf = StringIO.StringIO()
    with zipfile.ZipFile(mf, mode='w', compression=zipfile.ZIP_DEFLATED) as zf:
        zf.writestr('../../../../../../../../../var/www/html/ATutor/mods/poc/1111.phtml', code)
        zf.writestr('imsmanifest.xml', "noxmlhereoops")
    mf.write(zf)
    print mf.getvalue()
    return mf.getvalue()
_build_zip_inmem(code)  

这基本上有效,除了 getvalue() 似乎也返回对象的内存地址。打印行的最后输出显示<zipfile.ZipFile object at 0x7f4e8ba434d0>,我相信这就是我的 POST 失败的原因。

如何将此内存中的 zip 转换为可以通过 POST 发送的二进制文件?

谢谢!!!

【问题讨论】:

    标签: python python-requests stringio


    【解决方案1】:

    mf.getvalue()<zipfile.ZipFile object at 0x7f4e8ba434d0> 结尾,因为您在调用mf.write(zf) 时将该字符串写入mf。对write() 的调用是不必要的,因为with zipfile.ZipFile(mf, ...) 块已经将压缩文件写入mf

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-14
      • 2023-03-19
      • 2011-06-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多