python 2.4中的多部分二进制文件POST

时间:2012-01-25 20:48:18

标签: python binary multipart python-2.4

我已经检查了很多代码片段,但是我不知道如何在单个请求中使用python 2.4发布多部分文本和二进制文件?注释中的Here提到了有关BytesIO类的内容,但2.4中没有。 (普通的python,没有第三方库)谢谢。

1 个答案:

答案 0 :(得分:0)

使用Python 2.6,您可以使用请求库,这里是从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": ""
}