[Python] POST wav files with requests [POST]

at first

I uploaded the voice recognition API on Heroku, How can I post a wav file and get the recipient as well? Since there are few documents and trial and error, I will leave it as a memo.

Key, user id, pass, etc. are security measures.

Server side

from flask import Flask, request, abort
import soundfile as sf
from io import BytesIO
import os

app = Flask(__name__)

@app.route("/webapp", methods=['POST'])
def webapp():
    webaccesskey = request.headers["KEY"]
    webaccesspassword = request.headers["PWD"]
    if webaccesskey == "ok" and webaccesspassword == "foapkun":
        try:
            if request.args.get("TYPE") == "wav_message":
                user_id = request.args.get("USERID")
                key = request.args.get("KEY")
                valid = checker(user_id, key)
                if valid == True:
                    wavdata = request.files["file"].stream
                    audio_data, samplerate = sf.read(BytesIO(wavdata.read()))
                    #The process you want to put here
        except:
            abort(400)
    else:
        abort(400)
    return 'OK'

def checker(user_id, key):
    valid = False
    if user_id == "user_id":
        if key == "key":
            valid = True
    return valid


if __name__ == "__main__":
    port = int(os.getenv("PORT", 5000))
    app.run(host="0.0.0.0", port=port)

requests side

import requests

class Message:
    def __init__(self):
        self.accessURL = "http://localhost:5000/webapp"
        self.accesskey = "ok"
        self.accessPASSWORD = "qiita"

    def put_wav(self, user_id, key):
        self.headers = {"KEY": self.accesskey, "PWD": self.accessPASSWORD}
        self.payload = {"TYPE": "wav_message", "USERID": user_id, "KEY": key}
        fileName = 'test.wav'
        files = {'file': open(fileName, 'rb')}
        r = requests.post(self.accessURL, headers=self.headers, params=self.payload, files=files)
        print(r.text)

if __name__ == "__main__":
    message = Message()
    message.put_wav(user_id="user_id", key="key")

Recommended Posts

[Python] POST wav files with requests [POST]
Retry with python requests
Read wav files with only Python standard packages
POST json with Python3 script
Post to slack with Python 3
Sorting image files with Python (2)
Sort huge files with python
Sorting image files with Python (3)
Sorting image files with Python
Integrate PDF files with Python
Reading .txt files with Python
Retry post request using python requests
Recursively unzip zip files with python
Manipulating EAGLE .brd files with Python
Quine Post with Qiita API (Python)
Get the weather with Python requests
Get the weather with Python requests 2
Decrypt files encrypted with OpenSSL with Python 3
MP3 to WAV conversion with Python
Post multiple Twitter images with python
Handle Excel CSV files with Python
Read files in parallel with Python
Easily post to twitter with Python 3
Send multipart / form-data with python requests
[AWS] Using ini files with Lambda [Python]
Play audio files from Python with interrupts
POST variously with Python and receive with Flask
Post Test 3 (Working with PosgreSQL in Python)
Decrypt files encrypted with openssl from python with openssl
Reading and writing JSON files with Python
Download files on the web with Python
[Easy Python] Reading Excel files with openpyxl
Convert HEIC files to PNG files with Python
[Easy Python] Reading Excel files with pandas
Call APIGateWay with APIKey in python requests
Transcribe WAV files with Cloud Speech API
Statistics with python
Python with Go
Twilio with Python
Integrate with Python
Play with 2016-Python
AES256 with python
Tested with Python
python starts with ()
with syntax (Python)
Bingo with python
Zundokokiyoshi with python
Excel with Python
Microcomputer with Python
Cast with python
Post youtube soaring title to twitter with python3
Handle zip files with Japanese filenames in Python 3
Drag and drop local files with Selenium (Python)
Character encoding when dealing with files in Python 3
Download and import files with Splunk external python
Upload files to Google Drive with Lambda (Python)
Reading and writing fits files with Python (memo)
Wav file generation from numeric text with python
Convert multiple proto files at once with python
Post an article with an image to WordPress with Python
Zip, unzip with python