inflearn logo
강의

Course

Instructor

Python Concurrent Programming: From Data Collection to Web Development (feat. FastAPI, async, await)

Using Python Coroutines

파이썬 코루틴 사용하기 - aiohttp로 crawling시에 ssl error 발생

3156

gyuyoung park

14 asked

4

질문은 아니고, 에러가 발생하는 부분이 있어 나중에 문제를 겪고 있으신 분들에게 도움이 될 것 같아 남깁니다.

https://github.com/amamov/teaching-async-python/blob/main/1-%ED%8C%8C%EC%9D%B4%EC%8D%AC-%EC%BD%94%EB%A3%A8%ED%8B%B4%EA%B3%BC-%EB%B9%84%EB%8F%99%EA%B8%B0-%ED%95%A8%EC%88%98/04-2-coroutine-fetcher.py

위 링크에 있는 코드 입니다.

 # https://docs.aiohttp.org/en/stable/
# pip install aiohttp~=3.7.3


import aiohttp
import time
import asyncio


async def fetcher(session, url):
    async with session.get(url) as response:
        return await response.text()


async def main():
    urls = ["https://naver.com", "https://google.com", "https://instagram.com"] * 10

    async with aiohttp.ClientSession() as session:
        result = await asyncio.gather(*[fetcher(session, url) for url in urls])
        print(result)


if __name__ == "__main__":
    start = time.time()
    asyncio.run(main())
    end = time.time()
    print(end - start)  # 4.8

해당 코드 실행 시 다음의 ssl 에러가 발생합니다. 왜 그런지는 모르겠지만, requests에서는 ssl 에러가 발생하지 않는데 aiohttp에서는 발생합니다.

[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:997)')]

https://github.com/aio-libs/aiohttp/issues/955

에서 확인한 방법을 적용하면 쉽게 해결이 가능합니다.

 

ssl 검증 과정을 코드 상에서 false 처리하면 됩니다.

async with aiohttp.ClientSession(
        connector=aiohttp.TCPConnector(ssl=False)
    ) as session:

 

전체코드는 다음과 같습니다.

import aiohttp
import time
import asyncio


async def fetcher(session, url):
    async with session.get(url) as response:
        return await response.text()


async def main():
    urls = ["https://naver.com", "https://google.com", "https://instagram.com"] * 10

    async with aiohttp.ClientSession(
        connector=aiohttp.TCPConnector(ssl=False)
    ) as session:
        result = await asyncio.gather(*[fetcher(session, url) for url in urls])
        print(result)


if __name__ == "__main__":
    start = time.time()
    asyncio.run(main())
    end = time.time()
    print(end - start)

 

python FastAPI 동시성

Answer 2

0

sjm

좋은 글 감사합니다! 덕분에 해결했습니당 ^-^

0

johnywhisky

정보 공유 감사합니다 :)

대규모 크롤링 시 동시 요청 수 제어 방법

0

94

2

AWS LighSail 접근 불가

0

131

1

강의상의 readme와 배포되어 있는 것이 다른 것 같아요

0

96

1

book_scraper.py 에서 import get_secret 관련 질문

0

98

1

ThreadPoolExecutor 중간에 멈추는 법

0

291

1

몽고Db 기초 질문

0

194

1

async await 문법 질문

0

237

1

멀티 스레딩 질문..

0

193

1

교안 제공은 안되나요?

0

227

1

TypeError: field Config is defined without type annotation

0

257

1

RuntimeWarning: Enable tracemalloc to get the object allocation traceback

0

1707

1

jinja2templates

0

244

1

몽고DB 설정 관련 질문

0

242

1

비동기 함수를 동기적인 실행으로 만드는 부분 질문입니다

1

307

2

proxy 사용 질문

0

261

1

AWS Lightsail SSL handshake failed 오류 문의드립니다.

0

491

1

동시성과 병렬성

0

325

1

python select linter ? 오류

1

739

2

수업질문

1

351

1

response 객체에서 키값으로 조회 시 KeyError 발생

0

568

1

from config import get_secret 질문

0

385

1

파이썬 코루틴활용 영상 질문

0

279

1

가상환경 설정 질문

0

316

1

pydantic import error

0

1225

2