U Bhargava
U Bhargava

Reputation: 23

Code works from localhost but not on server - https://www.nseindia.com/api/equity-stockIndices?index=NIFTY%2050 - python

I am trying to access https://www.nseindia.com/api/equity-stockIndices?index=NIFTY%2050. It is working fine from my localhost (code compiled in vscode) but when I deploy it on the server I get HTTP 499 error. Did anybody get through this and was able to fetch the data using this approach? Looks like nse is blocking the request somehow. But then how is it working from a localhost?

P.S. - I am a paid user of pythonAnywhere (Hacker) subscription

import requests
import time

def marketDatafn(query):
    headers = {'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.87 Safari/537.36'}

    main_url = "https://www.nseindia.com/"

    session = requests.Session()

    response = session.get(main_url, headers=headers)
    
    cookies = response.cookies
    url = "https://www.nseindia.com/api/equity-stockIndices?index=NIFTY%2050"
    nifty50DataReq = session.get(url, headers=headers, cookies=cookies, timeout=15)

    nifty50DataJson = nifty100DataReq.json()

    return nifty50DataJson['data']

Upvotes: 0

Views: 666

Answers (1)

imxitiz
imxitiz

Reputation: 3987

Actually "Pythonanywhere" only supports those website which are in this whitelist.

And I have found that there are only two subdomain available under "nseindia.com", which is not that you are trying to request.

bricsonline.nseindia.com bricsonlinereguat.nseindia.com

So, pythonanywhere is blocking you to sent request to that website.

Here's the link to read more about how to request to add your website there.

Upvotes: 1

Related Questions