是否可以使用python请求库发布音频文件

问题描述 投票:1回答:1

我正在考虑使用Python Requests library将mp3发布到api,但是文档中所有发布文件的示例都是针对文本文件的。可以将这个库用于音频吗?

python rest python-requests
1个回答
3
投票

是,可以通过库发送任何字节序列:

with open(audiofile, 'rb') as fobj:
    requests.post(url, files={'fieldname', fobj})

实际上,first multipart-encoded file example文档中的requests发布了一个二进制文件:

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

>>> r = requests.post(url, files=files)
>>> r.text
{
  ...
  "files": {
    "file": "<censored...binary...data>"
  },
  ...
}
© www.soinside.com 2019 - 2024. All rights reserved.