12

I am trying aiohttp and asyncio first time and getting error like

[WinError 10054] An existing connection was forcibly closed by the remote host

but same url when i fetch with requests it working properly.so what does this error mean and how to solve it ?

my code

import aiohttp 
import asyncio

url = 'https://nseindia.com/api/historical/fo/derivatives?&from=24-01-2020&to=24-01-2020&expiryDate=06-FEB-2020&instrumentType=OPTIDX&symbol=NIFTY&csv=true'

headers = {"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36"}
async def get_data():
    async with aiohttp.ClientSession(headers=headers) as session:
        async with session.get(url) as resp:
            result = await resp.text()
            return result


async def main():
    result = await get_data()
    return result

asyncio.run(main())

gaurav
  • 1,281
  • 1
  • 13
  • 25
  • Link is dead now unfortunately. I get invalid parameters, maybe this should be deleted? – Cow Aug 17 '22 at 10:48
  • 1
    Alternatively, try using the `httpx` package instead. Also try using httpx.AsyncClient with `http2=True` – pepoluan Nov 18 '22 at 02:44

1 Answers1

2

This is due to your internet connection being dropped. You should ensure that your internet connection is stable. Additionally you could add a try except statement and try to send another request later.