Solved - OSError("(10060, 'WSAETIMEDOUT')" in Requests package in Python

Code which return error

import json
import requests
url = 'https://www1.nseindia.com'

r = requests.get(url)
print (r.text)

Error

Traceback (most recent call last):
  File "D:\PythonLearn\lib\site-packages\urllib3\connectionpool.py", line 601, in urlopen
    chunked=chunked)
  File "D:\PythonLearn\lib\site-packages\urllib3\connectionpool.py", line 387, in _make_request
    six.raise_from(e, None)
  File "<string>", line 2, in raise_from
  File "D:\PythonLearn\lib\site-packages\urllib3\connectionpool.py", line 383, in _make_request
    httplib_response = conn.getresponse()
  File "D:\PythonLearn\lib\http\client.py", line 1331, in getresponse
    response.begin()
  File "D:\PythonLearn\lib\http\client.py", line 297, in begin
    version, status, reason = self._read_status()
  File "D:\PythonLearn\lib\http\client.py", line 258, in _read_status
    line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
  File "D:\PythonLearn\lib\socket.py", line 586, in readinto
    return self._sock.recv_into(b)
  File "D:\PythonLearn\lib\site-packages\urllib3\contrib\pyopenssl.py", line 285, in recv_into
    raise SocketError(str(e))
OSError: (10060, 'WSAETIMEDOUT')

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:\PythonLearn\lib\site-packages\requests\adapters.py", line 449, in send
    timeout=timeout
  File "D:\PythonLearn\lib\site-packages\urllib3\connectionpool.py", line 639, in urlopen
    _stacktrace=sys.exc_info()[2])
  File "D:\PythonLearn\lib\site-packages\urllib3\util\retry.py", line 357, in increment
    raise six.reraise(type(error), error, _stacktrace)
  File "D:\PythonLearn\lib\site-packages\urllib3\packages\six.py", line 685, in reraise
    raise value.with_traceback(tb)
  File "D:\PythonLearn\lib\site-packages\urllib3\connectionpool.py", line 601, in urlopen
    chunked=chunked)
  File "D:\PythonLearn\lib\site-packages\urllib3\connectionpool.py", line 387, in _make_request
    six.raise_from(e, None)
  File "<string>", line 2, in raise_from
  File "D:\PythonLearn\lib\site-packages\urllib3\connectionpool.py", line 383, in _make_request
    httplib_response = conn.getresponse()
  File "D:\PythonLearn\lib\http\client.py", line 1331, in getresponse
    response.begin()
  File "D:\PythonLearn\lib\http\client.py", line 297, in begin
    version, status, reason = self._read_status()
  File "D:\PythonLearn\lib\http\client.py", line 258, in _read_status
    line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
  File "D:\PythonLearn\lib\socket.py", line 586, in readinto
    return self._sock.recv_into(b)
  File "D:\PythonLearn\lib\site-packages\urllib3\contrib\pyopenssl.py", line 285, in recv_into
    raise SocketError(str(e))
urllib3.exceptions.ProtocolError: ('Connection aborted.', OSError("(10060, 'WSAETIMEDOUT')",))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:\Share_Market_Tutorial\fetchData.py", line 5, in <module>
    r = requests.get(url)
  File "D:\PythonLearn\lib\site-packages\requests\api.py", line 76, in get
    return request('get', url, params=params, **kwargs)
  File "D:\PythonLearn\lib\site-packages\requests\api.py", line 61, in request
    return session.request(method=method, url=url, **kwargs)
  File "D:\PythonLearn\lib\site-packages\requests\sessions.py", line 530, in request
    resp = self.send(prep, **send_kwargs)
  File "D:\PythonLearn\lib\site-packages\requests\sessions.py", line 643, in send
    r = adapter.send(request, **kwargs)
  File "D:\PythonLearn\lib\site-packages\requests\adapters.py", line 498, in send
    raise ConnectionError(err, request=request)
requests.exceptions.ConnectionError: ('Connection aborted.', OSError("(10060, 'WSAETIMEDOUT')",))
[Finished in 258.0s]

Code which runs successfully

import json
import requests

url = 'https://www1.nseindia.com'

headers = {
    'cache-control': 'max-age=0',
    'connection': 'keep-alive',
    'upgrade-insecure-requests': '1',
    'accept': 'text/html,application/xhtml+xml,application/json',
    'user-agent': 'Mozilla/5.0'
    }

r = requests.get(url, headers=headers)
print (r.status_code)

Result
200
[Finished in 1.0s]

Comments

Popular posts from this blog

DVB Q&A

Comprehensive guide to Matplotlib