简体   繁体   中英

Python3.6 requests.post results in ConnectionError number 104, but all is fine when using curl

I am trying to rewrite a curl command to python. The idea is the following: we upload an audio file, and get in return what it was said in it in text - eg speech to text.

The site has provided us a curl example, which makes a post request and in return receives a simple response.

When I tried to convert the curl request into python one (with little help from https://curl.trillworks.com/ ) I get ConnectionError number 104, connection reset by peer.

I strongly suspect that this happens because when I make the connection vie curl firstly I get the response: < HTTP/1.1 100 Continue, and after short time of waiting another response: < HTTP/1.1 200 OK, with the some sample data. I think that python requests.post just hangs at the first HTTP/1.1 100.

I could not handle or reset the connection using try... except..., neither succeeded in looping it with time.sleep().

Any ideas?

PS CODE:

Curl command:

curl "LINK TO SERVER" -H "Content-Type: audio/x-speex;rate=16000" -H "Accept-Language: ENUS" -H "Transfer-Encoding: chunked" -H "Accept: application/xml" -H "Accept-Topic: Dictation" -k --data-binary @audio_16k16bit.pcm 

Python equivalent:

#!/bin/python

import requests
import time

headers = {
   'Content-Type': 'audio/x-wav;codec=pcm;bit=16;rate=16000',
   'Accept-Language': 'ENUS',
   'Transfer-Encoding': 'chunked',
   'Accept': 'application/xml',
    'Accept-Topic': 'Dictation',
}

params = (
    ('appId', ''),
     ('appKey',''),
('id', ''),
)

data = open('audio_16k16bit.pcm', 'rb').read()

r = requests.post('LINK TO SERVER', headers=headers, data=data, verify=False)

print(r.content)

SOLUTION

Should remove and let the server determine the type of file, also since we are sending it as one file chunked is not needed:

'Content-Type': 'audio/x-wav;codec=pcm;bit=16;rate=16000',
 'Transfer-Encoding': 'chunked'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM