request 모듈을 사용하여 get, post method를 사용하는 예시 코드는 아래와 같다.
url = 'https://www.tistory.com/api/get/url'
r = requests.get(url)
url = 'https://www.tistory.com/api/post/url'
data = {'key': 'value'}
r = requests.post(url, json = data)
추가로 각 request에 timeout 설정을 하려면 파라미터로 (timeout = n)을 추가한다. (sec)
url = 'https://www.tistory.com/api/post/url'
data = {'key':'value'}
r = requests.post(url, json=data, timeout=5)
기본 timeout은 connection timeout이므로 전체 응답 시간제한이 아니기 때문에, 서버가 응답을 설정시간 내에 보내지 않는 경우만 예외가 발생한며 설정할 timeout의 시간이 os의 tcp timeout 이상인 경우 효과가 없다.
connection timeout과 read timeout을 각각 설정하려면 아래와 같이 설정한다.
url = 'https://www.tistory.com/api/post/url'
data = {'key':'value'}
r = requests.post(url, json=data, timeout=(5, 3))
관련 자료 : https://requests.readthedocs.io/en/latest/user/advanced/#timeouts
Advanced Usage — Requests 2.28.2 documentation
Advanced Usage This document covers some of Requests more advanced features. Session Objects The Session object allows you to persist certain parameters across requests. It also persists cookies across all requests made from the Session instance, and will
requests.readthedocs.io
반응형
'IT > Python' 카테고리의 다른 글
[Python] json의 key, value에 따른 데이터 추출 (0) | 2023.03.10 |
---|---|
[Python] String에 변수 사용(f-string) (1) | 2022.11.13 |
[Python] 2차원 배열 정렬 방법 (0) | 2022.07.31 |
[Python] "return list.sort()"는 None 일 때 (0) | 2022.07.31 |
댓글