0

I am attempting to create a zip file in memory with python that I will then attach to a POST request to send with python-requests. Here is the function I wrote

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)  

This basically works except getvalue() seems to also return the memory address of the object. The ending output of the print line says <zipfile.ZipFile object at 0x7f4e8ba434d0> and I believe this is why my POST is failing.

How do I convert this in-memory zip to binary that can then be sent via POST?

Thanks!!!

D3l_Gato
  • 1,297
  • 2
  • 17
  • 25

1 Answers1

1

mf.getvalue() ends with <zipfile.ZipFile object at 0x7f4e8ba434d0> because you wrote that string to mf when you called mf.write(zf). This call to write() is unnecessary, as the zipfile was already written to mf by the with zipfile.ZipFile(mf, ...) block.

jwodder
  • 54,758
  • 12
  • 108
  • 124