Multipart binary file POST in python 2.4

858 views Asked by At

I already checked a lot of code snippets but i could not get how to post multipart both text and binary files in single request with only python 2.4? Here in comments mentioned something about BytesIO class but is not present in 2.4. (plain python, no third-party libraries) Thanks.

1

There are 1 answers

1
amirouche On

with Python 2.6 you can use requests library, here is snippet extracted from the documentation:

>>> url = 'http://httpbin.org/post'
>>> files = {'report.xls': open('report.xls', 'rb')}

>>> r = requests.post(url, files=files)
>>> r.text
{
  "origin": "179.13.100.4",
  "files": {
    "report.xls": "<censored...binary...data>"
  },
  "form": {},
  "url": "http://httpbin.org/post",
  "args": {},
  "headers": {
    "Content-Length": "3196",
    "Accept-Encoding": "identity, deflate, compress, gzip",
    "Accept": "*/*",
    "User-Agent": "python-requests/0.8.0",
    "Host": "httpbin.org:80",
    "Content-Type": "multipart/form-data; boundary=127.0.0.1.502.21746.1321131593.786.1"
  },
  "data": ""
}