Exception for ConnectionResetError: [Errno 54] Connection reset by peer

假装没事ソ 提交于 2019-12-14 02:24:31

问题


I cannot successfully build an exception for the error I am receiving:

ConnectionResetError: [Errno 54] Connection reset by peer

Here is my code:

try:
    for img in soup.findAll('img', {'class': 'thisclass'}):
        print('Image #:' + str(counter) + ' URL: ' + img['src'])
        myurl = img['src']
        urllib.request.urlretrieve(str(myurl), str(mypath)+str(foldername)+'/webp/'+str(foldername)+str(counter)+'.webp')
        im = Image.open(str(mypath)+str(foldername)+'/webp/'+ str(foldername) +str(counter)+'.webp').convert("RGB")
        im.save(str(mypath)+str(foldername)+'/jpg/'+ str(foldername) +str(counter)+'.jpg','jpeg')
        print('Downloaded Image ' + str(counter))
        counter = counter + 1
        sleep(random.uniform(1.3,2.4))

except requests.exceptions.ConnectionResetError:
    print('Handle Exception')

except requests.exceptions.ConnectionError:
    print('Handle Exception')

These exceptions I have tested do not work. Any suggestions on how I can successfully handle this error?

edit: text fix


回答1:


It seems urllib is throwing the exception. In python 3 ConnectionResetError is a built in exception, just use:

except ConnectionResetError:

You don't seem to be using the requests module in any case - that should have been your hint.



来源:https://stackoverflow.com/questions/40851926/exception-for-connectionreseterror-errno-54-connection-reset-by-peer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!