aiohttp.client_exceptions.ClientOSError: [WinError 10054] An existing connection was forcibly closed by the remote host
Asked Answered
C

1

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())

Cinnamon answered 3/2, 2020 at 7:27 Comment(2)
Link is dead now unfortunately. I get invalid parameters, maybe this should be deleted?Dermatitis
Alternatively, try using the httpx package instead. Also try using httpx.AsyncClient with http2=TrueCarson
S
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.

Shockproof answered 18/4, 2023 at 8:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.