将Curl转换为Python | IBM STT

时间:2018-12-14 08:12:24

标签: python python-requests

我正在尝试将下面的curl命令转换为python

curl -X POST -u "apikey:my_key" --header "Content-Type:" --data-binary @testaudio1.wav "https://gateway-lon.watsonplatform.net/speech-to-text/api/v1/recognize?speaker_labels=true"

curl命令是一个有效的命令,但是我无法获得对应于python的命令。

我尝试了以下代码

import json, sys
from os.path import join, dirname
from watson_developer_cloud import SpeechToTextV1

wav_file = sys.argv[1]

speech_to_text = SpeechToTextV1(
    iam_apikey='my_api',
    url='https://gateway-lon.watsonplatform.net/speech-to-text/api/v1'
)

with open(wav_file, 'rb') as audio:
    result = speech_to_text.recognize(audio, content_type='audio/wav', timestamps=True,
        word_confidence=True, speaker_labels=True)

但是它似乎抛出了错误AppData\Local\Continuum\Anaconda3\lib\socket.py", line 732, in getaddrinfo for res in _socket.getaddrinfo(host, port, family, type, proto, flags): socket.gaierror: [Errno 11001] getaddrinfo failed

1 个答案:

答案 0 :(得分:0)

使用requests模块。

例如:

import requests

with open(wav_file, 'rb') as audio:
    response = requests.post("https://gateway-lon.watsonplatform.net/speech-to-text/api/v1/recognize?speaker_labels=true",
                             headers={'apikey': 'my_key'},
                             data=audio))