Python3 requests ConnectionResetError(10054) when opening a picture

19,423

Solution 1

I know it isn't your case, and this is really old, but when searching google I stumbled on this, so I'll leave what solved my problem here:

test_link = "https://www.bbb.org/washington-dc-eastern-pa/business-reviews/online-education/k12-inc-in-herndon-va-190911943/#sealclick"
page = requests.get(test_link)

I got the error:

requests.exceptions.ConnectionError: ('Connection aborted.', ConnectionResetError(10054, 'An existing connection was forcibly closed by the remote host', None, 10054, None))

So it isn't multiple connections, I think the problem was the headers, once I put headers the error disappeared, this is the code afterwards:

test_link = "https://www.bbb.org/washington-dc-eastern-pa/business-reviews/online-education/k12-inc-in-herndon-va-190911943/#sealclick"
headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:66.0) Gecko/20100101 Firefox/66.0",
    "Accept-Encoding": "*",
    "Connection": "keep-alive"
}
page = requests.get(test_link, headers=headers)

Solution 2

I had this error when the server was hosted on my machine over https and the SSL certificate was not correctly installed.

Following instructions to properly install the server's certificates solved the problem:

https://coderead.wordpress.com/2014/08/07/enabling-ssl-for-self-hosted-nancy/ https://www.cloudinsidr.com/content/how-to-install-the-most-recent-version-of-openssl-on-windows-10-in-64-bit/

Share:
19,423
Jackoo
Author by

Jackoo

Updated on June 08, 2022

Comments

  • Jackoo
    Jackoo almost 2 years

    I was trying to download pictures from websites like 'http://xxx.jpg'.

    The code:

    headers={'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.139 Safari/537.36'}
    url='http://xxx.jpg'
    response = requests.get(url,headers=headers)
    downloadFunction()
    

    The error writes:

    requests.exceptions.ConnectionError: ('Connection aborted.', ConnectionResetError(10054, 'An existing connection was forcibly closed by the remote host', None, 10054, None))
    

    Error occurred at the first request, so it wasn't the request frequency which had caused the error. And I could still open the websites using a browser, so I just needed the code to act more like a browser. How can I achieve that besides setting the user-agent?

    • Stack
      Stack almost 6 years
      can you add the image link, it will help!
    • Tomalak
      Tomalak almost 6 years
      Here is what I think happens: You are trying to steal images. The webserver does not like that and stops talking to you. (Or said differently: The people owning the webserver have put measures into place that prevent random people from scraping all their stuff.)
    • Hanzy
      Hanzy over 5 years
      @Tomalak to be fair I just got a similar message and was brought here by a simple google search. But I wasn't trying to steal images or anything else. I was using an API key to request data from a service that fit within the user agreement. I had the same issue -- still not certain the cause.
    • Tomalak
      Tomalak over 5 years
      Hm... Does it work reliably when you test your API call in some other tool (e.g. Postman, Advanced REST Client or even the browser console itself)? Are you adhering to the documentation? Can it be that you''re rate-limited? Does it work on a different machine/in a different network (to rule out firewall or proxy issues)?