Skip to content

Python:requests

Requests is an Apache2 Licensed HTTP library, written in Python, for human beings.

Python’s standard urllib2 module provides most of the HTTP capabilities you need, but the API is thoroughly broken. It was built for a different time — and a different web. It requires an enormous amount of work (even method overrides) to perform the simplest of tasks.

Install

$ pip install request

http method

아래와 같이 http method 별로 다양하게 사용할 수 있습니다.

r = requests.put("http://httpbin.org/put")
r = requests.delete("http://httpbin.org/delete")
r = requests.head("http://httpbin.org/get")
r = requests.options("http://httpbin.org/get")

Max retries

import requests
from requests.adapters import HTTPAdapter
from requests.packages.urllib3.util.retry import Retry

session = requests.Session()
retry = Retry(connect=3, backoff_factor=0.5)
adapter = HTTPAdapter(max_retries=retry)
session.mount('http://', adapter)
session.mount('https://', adapter)

session.get(url)

Example

Things shouldn’t be this way. Not in Python.

import requests
r = requests.get('https://api.github.com/user', auth=('user', 'pass'), timeout=10)
print r.content     # Content
print r.status_code # 200
print r.headers['content-type'] # 'application/json; charset=utf8'
print r.encoding # 'utf-8'
print r.text     # u'{"type":"User"...'
print r.json()   # {u'private_gists': 419, u'total_private_repos': 77, ...}

Authentication

HTTPBasicAuth

Many web services that require authentication accept HTTP Basic Auth. This is the simplest kind, and Requests supports it straight out of the box.

Making requests with HTTP Basic Auth is very simple:

from requests.auth import HTTPBasicAuth
basic = HTTPBasicAuth('user', 'pass')
requests.get('https://httpbin.org/basic-auth/user/pass', auth=basic)

In fact, HTTP Basic Auth is so common that Requests provides a handy shorthand for using it:

requests.get('https://httpbin.org/basic-auth/user/pass', auth=('user', 'pass'))

HTTPDigestAuth

Another very popular form of HTTP Authentication is Digest Authentication, and Requests supports this out of the box as well:

from requests.auth import HTTPDigestAuth
url = 'https://httpbin.org/digest-auth/auth/user/pass'
requests.get(url, auth=HTTPDigestAuth('user', 'pass'))

OAuth1

A common form of authentication for several web APIs is OAuth. The requests-oauthlib library allows Requests users to easily make OAuth 1 authenticated requests:

import requests
from requests_oauthlib import OAuth1
url = 'https://api.twitter.com/1.1/account/verify_credentials.json'
auth = OAuth1('YOUR_APP_KEY', 'YOUR_APP_SECRET', 'USER_OAUTH_TOKEN', 'USER_OAUTH_TOKEN_SECRET')
requests.get(url, auth=auth)

urllib으로 헬스 체크

python -c "import urllib.request as r; exit(0 if r.urlopen('http://www.python.org/').status == 201 else 1)"

See also

Favorite site