0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【responder】http通信で音声ファイルを送受信する

Last updated at Posted at 2020-12-10

これは何

ローカルにある.wavファイルをリモート のサーバーにhttpで送信したい。
そのサンプル用に作ったファイルの紹介です。

プログラムとしては何をしているの?

音声ファイルをhttpで送信するには、base64でバイナリに変換してから送る方法があるそうです。

リクエスト側

import base64
import requests


def post_file():
    url = "http://localhost:5042"
    file_name = 'sample.wav'

    with open(file_name, 'rb') as f:
        data = f.read()

    data_bytes = base64.b64encode(data)
    files = {'file': data_bytes}

    requests.post(url, data=files)


if __name__ == '__main__':
    post_file()

ホスト側

import base64
import responder

api = responder.API()


@api.route("/")
async def on_post(req, resp):
    data = await req.media()
    data_bytes = base64.b64decode(data['file'].encode())

    with open("wav_file.wav", 'bw') as f:
        f.write(data_bytes)


if __name__ == '__main__':
    api.run()
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?