티스토리 뷰
requests를 기본 문법으로 사용하면 일반 적인 경우에는 문제가 되지 않지만, http 호출을 자주 하게 되면 exceptions.MaxRetryError가 발생하는 경우가 있습니다.
이럴때는 request 의 session을 이용해서 connection을 재사용할 수 있도록 설정해 주면 문제를 해결할 수 있습니다. pool_connections, pool_maxsize, max_retries 를 추가해서 설정할 수 있습니다.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
def basic_get(url): | |
response = requests.get(url) | |
print(response.text) | |
def session_get(url): | |
rs = requests.session() | |
rs.mount('http://', requests.adapters.HTTPAdapter(pool_connections=3, pool_maxsize=10, max_retries=3)) | |
rs.mount('https://', requests.adapters.HTTPAdapter(pool_connections=3, pool_maxsize=10, max_retries=3)) | |
rs.headers = {'Content-Type': 'application/json'} | |
response = rs.get(url) | |
print(response.text) | |
if __name__ == '__main__': | |
basic_get("http://www.naver.com") | |
session_get("http://www.naver.com") |
반응형
'python' 카테고리의 다른 글
[python] pandas import 시 UserWarning: Could not import the lzma module. Your installed Python is incomplete 오류 (0) | 2023.03.21 |
---|---|
[python] request 함수를 사용하여 오류가 발생했을 때 재작업하는 방법 (0) | 2022.11.16 |
[python] http를 이용한 post 구현 (0) | 2022.05.17 |
[python] datetime 변환시 타임존을 설정해야 하는 이유 (0) | 2022.05.17 |
[python] 우분투에 파이썬 3.7 설치 (0) | 2021.05.27 |
반응형
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- build
- hbase
- S3
- 다이나믹
- 하이브
- airflow
- java
- 정올
- 파이썬
- bash
- oozie
- mysql
- nodejs
- emr
- HIVE
- 오류
- 백준
- yarn
- Hadoop
- 하둡
- SQL
- ubuntu
- Tez
- SPARK
- HDFS
- Linux
- AWS
- Python
- 알고리즘
- error
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
글 보관함