Python error for request.get
Question: I am trying to write a Python script which enables me to acces a webpage and
download a file from that page. My first attempt was to simply get to that
page and i tried the following code:

    
    
    import requests
    
    url = 'https://www.google.com/?gws_rd=ssl' #using google as an example
    
    r = requests.get(url)
    
    print(r.url)
    

I am given this error:

    
    
    runfile('C:/Users/ME/Desktop/TMS502.py', wdir='C:/Users/ME/Desktop')
    Traceback (most recent call last):
    
      File "<ipython-input-23-bc585dcceef8>", line 1, in <module>
        runfile('C:/Users/ME/Desktop/TMS502.py', wdir='C:/Users/ME/Desktop')
    
      File "C:\Users\ME\AppData\Local\Continuum\Anaconda\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 585, in runfile
        execfile(filename, namespace)
    
      File "C:/Users/ME/Desktop/TMS502.py", line 16, in <module>
        r = requests.get(url)
    
      File "C:\Users\ME\AppData\Local\Continuum\Anaconda\lib\site-packages\requests\api.py", line 55, in get
        return request('get', url, **kwargs)
    
      File "C:\Users\ME\AppData\Local\Continuum\Anaconda\lib\site-packages\requests\api.py", line 44, in request
        return session.request(method=method, url=url, **kwargs)
    
      File "C:\Users\ME\AppData\Local\Continuum\Anaconda\lib\site-packages\requests\sessions.py", line 456, in request
        resp = self.send(prep, **send_kwargs)
    
      File "C:\Users\ME\AppData\Local\Continuum\Anaconda\lib\site-packages\requests\sessions.py", line 559, in send
        r = adapter.send(request, **kwargs)
    
      File "C:\Users\ME\AppData\Local\Continuum\Anaconda\lib\site-packages\requests\adapters.py", line 375, in send
        raise ConnectionError(e, request=request)
    
    ConnectionError: HTTPSConnectionPool(host='www.google.com', port=443): Max retries exceeded with url: /?gws_rd=ssl (Caused by <class 'socket.error'>: [Errno 10054] An existing connection was forcibly closed by the remote host)
    

Can someone please help me?


Answer: You are getting that error because the remote side (in this case Google) is
closing your requests or you are otherwise no longer able to establish a
connection to it.

From the error:

    
    
    ConnectionError: HTTPSConnectionPool(host='www.google.com', port=443): 
    Max retries exceeded with url: /?gws_rd=ssl 
    (Caused by <class 'socket.error'>: [Errno 10054] An existing connection was forcibly closed by the remote host)
    

We can look into the
[source](https://github.com/kennethreitz/requests/blob/8b5e457b756b2ab4c02473f7a42c2e0201ecc7e9/requests/packages/urllib3/exceptions.py#L62-L78)
for a hint:

    
    
    class MaxRetryError(RequestError):
        """Raised when the maximum number of retries is exceeded.
        :param pool: The connection pool
        :type pool: :class:`~urllib3.connectionpool.HTTPConnectionPool`
        :param string url: The requested Url
        :param exceptions.Exception reason: The underlying error
        """
    
        def __init__(self, pool, url, reason=None):
            self.reason = reason
    
            message = "Max retries exceeded with url: %s (Caused by %r)" % (
                url, reason)
    
            RequestError.__init__(self, pool, url, message)
    

Try another example host and your code should work, such as
<https://example.org>.

The error message "An existing connection was forcibly closed by the remote
host" is coming from your operating system (Windows) and Requests is showing
you this text in an attempt to be helpful.

