Python

[Python3] requests library SSLError: EOF occurred in violation of protocol

제임스는거위 2023. 3. 8. 13:29

최근 파이썬에서 requestsBeautifulSoup을 활용해 특정 웹사이트의 여러 웹 페이지 콘텐츠를 긁어오는 작업을 진행중이다.

 

미리 작성해둔 json 파일을 열고, 

여러 사이트의 글 목록이 있는 url(html 또는 xml) 각 사이트의 컨텐츠 리스트를 추출한 뒤

각 페이지의 콘텐츠를 추출하는 방식으로 코드를 짰다.

 

그런데 어제까진 잘 작동하던 코드가 별다른 변경사항이 없었음에도 갑작스럽게 다음과 같은 에러가 떴다.

SSLError: EOF occurred in violation of protocol

 

이를 어쩌나, 방법들을 찾아보았다.

 

방법 1. 필요한 라이브러리 설치

첫 번째 방법은 SSL 오류와 연관된 라이브러리들을 설치해주는 것이다.

 

Python requests.exceptions.SSLError: EOF occurred in violation of protocol

I would retrieve some information from an ABB G13 gateway that offer a RESTful JSON API. API is hosted by the gateway via https endpoint. Basic authentication mechanism is used for authentication.

stackoverflow.com

pip install ndg-httpsclient

pip install pyopenssl

pip install pyasn1

 

 

방법 2. Requests 설정

두 번째 방법은 get을 사용할 때 Verify 변수를 False로 설정해주는 것이다.

requests.get(url, verify=False)

 

방법 3. 세션 설정

세 번째 방법은 세션을 설정해주는 것이다. 

만약 한 서버에 여러번 get 요청을 보내면 서버에 큰 부하를 주게 되고, 그 경우에 서버측에서 내 requests를 막아버린다는 것이다.

네트워크에 관한 지식이 일천해 정확한 원리는 모르겠으나, 어찌됐든 세션을 설정해서 max_retries를 20으로 설정해주면 해결된다.

 

SSLError: EOF occurred in violation of protocol (_ssl.c:590) · Issue #3391 · psf/requests

Please bear with me as I'm quite new with Python and github in general. I have been using requests to scrape data from the Play Store. I need to make a large amount of requests (about 20k). It ...

github.com

import requests
sess = requests.Session()
adapter = requests.adapters.HTTPAdapter(max_retries = 20)
sess.mount('http://', adapter)