junzishiwo
南昊天
采纳率0%
2021-03-01 17:15

bytes can only contain ASCII literal characters

#-*- coding: utf-8 -*-
import requests
import time
import hashlib
import base64
import os,sys

URL = 'http://openapi.xfyun.cn/v2/aiui'
APPID = "564546"
API_KEY = "32dd75e4fa9067b2609dfa2b8a1daf2e"
AUE = "raw"
AUTH_ID = "2894c985bf8b1111c6728db79d3479ae"
DATA_TYPE = "audio"
SAMPLE_RATE = "16000"
SCENE = "main"
RESULT_LEVEL = "complete"
LAT = "39.938838"
LNG = "116.368624"
#个性化参数,需转义
PERS_PARAM = "{\\\"auth_id\\\":\\\"2894c985bf8b1111c6728db79d3479ae\\\"}"
FILE_PATH = b"E:/讯飞sdk/音频文件"


def buildHeader():
    curTime = str(int(time.time()))
    param = "{\"result_level\":\""+RESULT_LEVEL+"\",\"auth_id\":\""+AUTH_ID+"\",\"data_type\":\""+DATA_TYPE+"\",\"sample_rate\":\""+SAMPLE_RATE+"\",\"scene\":\""+SCENE+"\",\"lat\":\""+LAT+"\",\"lng\":\""+LNG+"\"}"
    #使用个性化参数时参数格式如下:
    #param = "{\"result_level\":\""+RESULT_LEVEL+"\",\"auth_id\":\""+AUTH_ID+"\",\"data_type\":\""+DATA_TYPE+"\",\"sample_rate\":\""+SAMPLE_RATE+"\",\"scene\":\""+SCENE+"\",\"lat\":\""+LAT+"\",\"lng\":\""+LNG+"\",\"pers_param\":\""+PERS_PARAM+"\"}"
    paramBase64 = base64.b64encode(param)

    m2 = hashlib.md5()
    m2.update(API_KEY + curTime + paramBase64)
    checkSum = m2.hexdigest()

    header = {
        'X-CurTime': curTime,
        'X-Param': paramBase64,
        'X-Appid': APPID,
        'X-CheckSum': checkSum,
    }

    return header

def readFile(filePath):
    binfile = open(filePath, 'rb')
    #str = 'this is fujieace.com test'
    #str = str.encode()
    #os.write(fd,str)
    #os.close(fd)
    data = binfile.read()
    return data

r = requests.post(URL, headers=buildHeader(),data=readFile(FILE_PATH))
print(r.content)

出现错误 bytes can only contain ASCII literal characters

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • qq_38084396 芫尘梦 1月前

    FILE_PATH = b"E:/讯飞sdk/音频文件"  改成 FILE_PATH = "E:/讯飞sdk/音频文件"

    点赞 评论 复制链接分享

为你推荐