0

我正在尝试处理来自 reddit api 服务的“HTTP ERROR 502”,因为它超出了限制

所以我试图通过以下代码片段重新连接它:

import urllib
import urllib.request
from urllib.error import URLError, HTTPError
from urllib.request import Request, urlopen

try:
    with urllib3.request.urlopen(submission.url) as url:
        arr = np.asarray(bytearray(url.read()), dtype=np.uint8)
        rgb_img = cv2.imdecode(arr, -1) 
        cv2.imwrite(os.path.join('image_data',str(unique_name)+".jpg"),rgb_img)

except HTTPError as e:
    if e.code == 502:

        @retry(urllib.URLError, tries=4, delay=3, backoff=2)
         def urlopen_with_retry():
             return urllib.urlopen(submission.url)

          urlopen_with_retry()

    else:                    
        print ('Failure: ' + str(e.reason))

但这不起作用,并且在 urllib中找不到“重试”的错误

如何正确处理这个 HTTP 500 ish 错误?

4

0 回答 0