Akhil Pakkath
Akhil Pakkath

Reputation: 293

TimeoutError: [Errno 60] Operation timed out

Following code/s is giving TimeoutError: [Errno 60] Operation timed out.

The url is working fine when accessed via Chrome or Postman.

url : https://www.nseindia.com/api/option-chain-indices?symbol=NIFTY

option1

import http.client

conn = http.client.HTTPSConnection("www.nseindia.com")
payload = ''
headers = {}
try:
    conn.request("GET", "/api/option-chain-indices?symbol=NIFTY", payload, headers)
    res = conn.getresponse()
    data = res.read()
    print(data.decode("utf-8"))
except http.client.error as e:
    print("exception occurred", e)

option2

import requests

url = "https://www.nseindia.com/api/option-chain-indices?symbol=NIFTY"
payload = {}
headers = {}

try:
    response = requests.request("GET", url, headers=headers, data=payload)
    response.raise_for_status()
    print(response.text.encode('utf8'))
except requests.HTTPError as exception:
    print("exception occurred", exception)

error : requests.exceptions.ReadTimeout: HTTPSConnectionPool(host='www.nseindia.com', port=443): Read timed out. (read timeout=None)

Upvotes: 0

Views: 4971

Answers (1)

Krishna
Krishna

Reputation: 26

Below code works for me. I am using Python 3.8.

import requests
import time
import pprint

def fire_get_request(url, headers, timeout=(5,25)):
    start_time = time.time()
    response=requests.get(url, timeout=timeout,headers=headers)
    end_time=time.time() - start_time
    return {'responsetime':end_time,'response':response.text}

url = "https://www.nseindia.com/api/quote-equity?symbol=DHFL&section=trade_info"
# url = "https://httpbin.org/user-agent"
headers = {'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.106 Safari/537.36'
            ,"accept": "application/json"
            }
pprint.pprint(fire_get_request(url=url, headers=headers))

Upvotes: 1

Related Questions