首页 > 解决方案 > 中途接收到 int() 和 ChunkedEncodingError: ('Connection broken: IncompleteRead..') 的错误无效文字?

问题描述

我每天都在使用 API &requests 库以固定的节奏读取一些数据,直到最近一切正常。突然在读取数据中途出错:

ValueError: invalid literal for int() with base 16: b''
During handling of the above exception, another exception occurred:    
ChunkedEncodingError: ('Connection broken: IncompleteRead(0 bytes read)', IncompleteRead(0 bytes read))
During handling of the above exception, another exception occurred:
ProtocolError: ('Connection broken: IncompleteRead(0 bytes read)', IncompleteRead(0 bytes read))

这不会偶尔发生。当我尝试读取数据时,我在..online 大约一个小时内收到此错误,我看到它与服务器/网络相关。但我不确定如何确认这一点或检查我是否无法解决它。

url = "https://api...."
dflist=[]
for i in range (0,variable1):


  with open("/dbfs/tmp/file{}.json".format(i+1)) as f:
    data = json.load(f)
    data1=json.dumps(data) 
    payload=str(data).encode("utf-8") 
    headers = {
      'content-type': "application/json; charset=utf-8",
      'cache-control': "no-cache",

        }
    #response = requests.request("POST", url, data=payload, headers=headers)
    with requests.request("POST", url, data=payload, headers=headers) as response:
      print("RESPONSE: ", response)
      for j in range(20):

        geturl= str(response.text)
        getresponse = requests.request("GET", geturl)
        if getresponse.status_code!=200:
          time.sleep(20)# wait 20 seconds 
          j=j+1

        else:
          jsondata=getresponse.text
          jdata=json.loads(jsondata)
          df=pd.DataFrame(jdata)
          print("df length")
          print(len(df))
          dflist.append(df)
          break

  i=i+1

另外,如果有人可以就如何解决此类问题提供建议,我将不胜感激?我不经常使用 API 和 url 请求,所以不确定在这种情况下检查的基础知识

标签: pythonpython-requestsrequesturllib

解决方案


推荐阅读