今天使用服务器,接入百度AI提供的自然语言处理-情感倾向分析功能时,发现运行文件完全无反应。
设断点后查明是:
host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id='+App_Key + '&client_secret=' + Secret_Key
response = requests.get(host)
res = requests.post(url=url, data=json.dumps(body), headers=header, verify=False)
这两句运行无反应。
后加了timeout = 3,就可以了
host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id='+App_Key + '&client_secret=' + Secret_Key
response = requests.get(host, timeout = 3)
res = requests.post(url=url, data=json.dumps(body), headers=header, verify=False, timeout = 3)
虽然不清楚原理,但可以成功运行。
后续:
运行代码期间,发现过程中容易timeout,又把timeout的数值调大,从3调到10,没有之前那么容易掉了。