Http error 429 too many requests python

An example of how to use tenacity to retry HTTP 429 errors in Python - GitHub - alexwlchan/handling-http-429-with-tenacity: An example of how to use tenacity to retry HTTP 429 errors in Python

Handling HTTP 429 errors in Python

This repo contains an example of how to handle HTTP 429 errors with Python.
If you get an HTTP 429, wait a while before trying the request again.

What is HTTP 429?

The HTTP 429 status code
means «Too Many Requests», and it’s sent when a server wants you to slow
down the rate of requests.

The 429 status code indicates that the user has sent too many
requests in a given amount of time («rate limiting»).

The response representations SHOULD include details explaining the
condition, and MAY include a Retry-After header indicating how long
to wait before making a new request.

For example:

HTTP/1.1 429 Too Many Requests
Content-Type: text/html
Retry-After: 3600

One way to handle an HTTP 429 is to retry the request after a short delay,
using the Retry-After header for guidance (if present).

How do you handle it in Python?

The tenacity library has some functions
for handling retry logic in Python. This repo has an example of how to use
tenacity to retry requests that returned an HTTP 429 status code.

The example uses urllib.request from the standard
library, but this approach can be adapted for other HTTP libraries.

There are two functions in handle_http_429_errors.py:

  • retry_if_http_429_error() can be used as the retry keyword of
    @tenacity.retry. It retries a function if the function makes an
    HTTP request that returns a 429 status code.
  • wait_for_retry_after_header(underlying) can be used as the wait keyword
    of @tenacity.retry. It looks for the Retry-After header in the HTTP response,
    and waits for that long if present. If not, it uses the supplied fallback strategy.

Example code

In the example below, the get_url() function tries to request a URL. If it
gets an HTTP 429 response, it waits up to three times before erroring out —
either respecting the Retry-After header from the server, or 1 second between
consecutive requests if not.

import urllib.request

from tenacity import retry, stop_after_attempt, wait_fixed

from handle_http_429_errors import (
    retry_if_http_429_error,
    wait_for_retry_after_header
)


@retry(
    retry=retry_if_http_429_error(),
    wait=wait_for_retry_after_header(fallback=wait_fixed(1)),
    stop=stop_after_attempt(3)
)
def get_url(url):
    return urllib.request.urlopen(url)


if __name__ == "__main__":
    get_url(url="https://httpbin.org/status/429")

Reader caution

I wrote this as a proof-of-concept in a single evening. It’s not
rigorously tested, but hopefully gives an idea of how you might do this if you
wanted to implement it properly.

Я пытаюсь использовать Python для входа на сайт и сбора информации с нескольких веб-страниц, и я получаю следующую ошибку:

Traceback (most recent call last):
  File "extract_test.py", line 43, in <module>
    response=br.open(v)
  File "/usr/local/lib/python2.7/dist-packages/mechanize/_mechanize.py", line 203, in open
    return self._mech_open(url, data, timeout=timeout)
  File "/usr/local/lib/python2.7/dist-packages/mechanize/_mechanize.py", line 255, in _mech_open
    raise response
mechanize._response.httperror_seek_wrapper: HTTP Error 429: Unknown Response Code

Я использовал time.sleep(), и он работает, но кажется неразумным и ненадежным, есть ли другой способ уклониться от этой ошибки?

Здесь мой код:

import mechanize
import cookielib
import re
first=("example.com/page1")
second=("example.com/page2")
third=("example.com/page3")
fourth=("example.com/page4")
## I have seven URL I want to open

urls_list=[first,second,third,fourth]

br = mechanize.Browser()
# Cookie Jar
cj = cookielib.LWPCookieJar()
br.set_cookiejar(cj)

# Browser options 
br.set_handle_equiv(True)
br.set_handle_redirect(True)
br.set_handle_referer(True)
br.set_handle_robots(False)

# Log in credentials
br.open("example.com")
br.select_form(nr=0)
br["username"] = "username"
br["password"] = "password"
br.submit()

for url in urls_list:
        br.open(url)
        print re.findall("Some String")

01 апр. 2014, в 14:44

Поделиться

Источник

3 ответа

Получение статуса 429 не является ошибкой, другой сервер «любезно» просит вас прекратить запросы на рассылку спама. Очевидно, что ваша ставка запросов слишком высока, и сервер не согласен с этим.

Вы не должны пытаться «уклониться» от этого или даже пытаться обойти настройки безопасности сервера, пытаясь обмануть свой IP-адрес, вы должны просто уважать ответ сервера, не отправляя слишком много запросов.

Если все настроено правильно, вы также получите заголовок «Retry-after» вместе с ответом 429. Этот заголовок указывает количество секунд, которое вы должны дождаться, прежде чем совершать другой вызов. Правильный способ справиться с этой «проблемой» состоит в том, чтобы прочитать этот заголовок и спящий ваш процесс на протяжении многих секунд.

Дополнительную информацию о статусе 429 вы можете найти здесь: http://tools.ietf.org/html/rfc6585#page-3

MRA
29 апр. 2014, в 14:15

Поделиться

Написание этой части кода исправило мою проблему:

requests.get(link, headers = {'User-agent': 'your bot 0.1'})

tadm123
03 нояб. 2016, в 04:17

Поделиться

Другим обходным решением было бы обмануть ваш IP-адрес с помощью какой-либо сети Public VPN или Tor. Это предполагает ограничение скорости на сервере на уровне IP.

Существует короткое сообщение в блоге, демонстрирующее способ использования tor вместе с urllib2:

http://blog.flip-edesign.com/?p=119

Gaurav Agarwal
01 апр. 2014, в 14:27

Поделиться

Ещё вопросы

  • 1NullPointerException при импорте XML-файла в Processing, Java
  • 0Как я могу создать короткое перенаправление домена на YouTube в htaccess?
  • 0Получить JSONArray с помощью StringRequest из Android
  • 0Мой sql фильтр по последней дате
  • 0Создается только первое динамически генерируемое поле выбора
  • 1Как создать связанные мультиселектные блоки в extjs classic 6
  • 1PyTest: автоматическое удаление временного каталога, созданного с помощью tmpdir_factory
  • 1Приложение Struts 2 выдает исключение 404
  • 1почему модуль re python2 не может идентифицировать символ u’® ‘
  • 1Изменение конфигурации цвета в веб-приложении во время выполнения
  • 1Логическое объяснение
  • 1Hibernate принудительное взаимодействие с «центральной» сущностью
  • 0Смещение электронной почты в HTML в Outlook 2013?
  • 1Застрял на регулярное выражение в C #
  • 0Ошибка таблицы вставки конвейера данных AWS с S3 в MySQL
  • 1Я использую random.shuffle неправильно или это ошибка?
  • 0JavaScript IF ELSE оператор в строке
  • 0показывает Jquery UI DatePicker по нажатию asp: linkbutton
  • 0Функция синхронизации в JavaScript
  • 1Поместите Google Maps Maker x Расстояние с y Подшипником?
  • 0ИЛИ условие в регулярном выражении
  • 0PHP: Https Curl с использованием прокси
  • 1Переменные из файла свойств в Ant
  • 1Android Easy Switch цветовая схема применения
  • 0Есть ли способ проверить, что файл содержит класс, интерфейс или функцию, прежде чем включать его в FileReflection ()?
  • 0Плавающая и ширина 100%
  • 1Получение NullPointerException при разборе JSONArray
  • 0вызвать функцию через jquery и вывести результаты в div в бритве html
  • 1Как объединить все значения определенного атрибута в списке?
  • 1Не удается импортировать сертификаты настроек Azure PUSBLISH в приложение Windows Phone 8.1
  • 0Перенос кода Matlab на C ++ с набором данных .mat
  • 0JQuery изменить размер застрял в цикле?
  • 0цикл полученной строки JSON, чтобы получить значения по JQuery
  • 0Соединение вашего приложения с базой данных
  • 0как выбрать вариант на основе стоимости
  • 1Как удалить файлы в AppData / Temp после загрузки файла с помощью .NET WebAPI 2? Ошибка файла в использовании
  • 0Я не могу показать несколько дней отсутствия
  • 0Jquery загрузка изображения во время загрузки изображения
  • 0jQueryUI прокручивает к началу ошибки
  • 0Выберите контрольный поиск на основе полей данных
  • 1Цветная гистограмма в xcharts
  • 1Десериализатор JSON.Net не генерирует исключение при передаче недопустимого значения для объекта
  • 1Python — получение ошибки SSL при попытке очистки веб-страницы
  • 1XAML Цвет фона Черный во время выполнения
  • 1Динамическая засыпка фрейма данных Pandas на основе указанной глубины
  • 1RxJS — значение выброса после получения n
  • 1Как изменить цвет текста при нажатии кнопки?
  • 0VHOST 404 Ошибка после изменения URL
  • 1JS RegEx замена не захваченной группы?
  • 1Что происходит с объектами, созданными внутри оператора using?

Сообщество Overcoder

Requests — это модуль для языка Python, который используют для упрощения работы с HTTP-запросами. Он удобнее и проще встроенного Urllib настолько, что даже в документации Python рекомендовано использовать Requests.

Установка библиотеки Requests

Самый простой вариант установки сторонних пакетов в Python — использовать pip — систему управления пакетами. Обычно pip предустанавливается вместе с интерпретатором. Если его нет — можно скачать. Для этого нужно ввести в командную строку:

Linux / MacOS

python -m ensurepip --upgrade

Windows

py -m ensurepip --upgrade

Когда pip установлен, для установки модуля Requests нужно ввести команду:

pip install requests

Как настроить библиотеку Requests. Библиотека не требует дополнительной настройки — ею можно сразу же пользоваться.

Начало работы. Давайте рассмотрим пример простейшего запроса в модуле Requests:

import requests # делаем запрос на чтение страницы https://sky.pro/media/
response = requests.get('https://sky.pro/media/')
print(response.ok)  # проверяем успешен ли запрос?
print(response.text)  # выводим полученный ответ на экран

А вот как сделать то же самое, но при помощи встроенной библиотеки Urllib:

from urllib.request import urlopen

# открываем запрос на чтение страницы http://sky.pro/media
with urlopen('http://sky.pro/media') as response:
   response_status = response.status  # сохраняем статус запроса в переменную
   html = response.read()  # вычитываем ответ в переменную

print(response_status == 200)  # проверяем успешен ли запрос
print(html.decode())  # выводим полученный ответ на экран

Модуль Requests в Python упрощает и автоматизирует многие действия, которые в стандартной библиотеке надо делать самостоятельно. Именно за это её любят и используют многие разработчики.

Давайте разберёмся, как работать с Requests, и из чего состоят HTTP-запросы.

Методы HTTP-запросов

HTTP — это протокол передачи информации в интернете. Он описывает правила и формат общения между двумя сторонами. Например, как браузеру описать запрос, а серверу — сформировать ответ. HTTP — это текстовый протокол, поэтому его может прочитать и человек.

Давайте разберем простейший запрос:

GET /media/ HTTP/1.1
Host: sky.pro

Первая строка формирует запрос: мы говорим серверу, что хотим прочитать (GET) ресурс по адресу /media/. В конце указывается версия протокола: HTTP/1.1.

Начиная со второй строки передается дополнительная информация, которая называется заголовками. Она опциональная — кроме заголовка Host. Он указывает домен, на котором находится запрашиваемый ресурс.

HTTP-ответ выглядит аналогично:

HTTP/1.1 200 OK
Content-Type: text/html
<тело ответа>

В первой строке указывается версия протокола и код ответа — статус, который описывает результат запроса. В следующих строках, так же, как и в запросе, перечисляются заголовки. В данном случае сервер говорит, что в ответе находится HTML-страница (Content-Type: text/html).

И в самом конце находится тело ответа: файл, HTML-страница или ничего. Браузер отрисовывает тело ответа — это уже то, что видит человек, когда загружает страницу.

Методы HTTP-запросов нужны, чтобы объяснить серверу, какое действие мы хотим совершить над ресурсом. Ресурс — это цель HTTP-запроса. Это может быть документ, фотография или просто веб-страница.

Разберем на примерах распространённые методы — в чём их суть и чем они отличаются. Важно: ниже разбираются механизмы работы каждого метода в том виде, в котором они описаны в спецификации. На практике поведение может отличаться, но такое встречается нечасто.

OPTIONS

Метод OPTIONS нужен, чтобы спросить сервер о том, какие методы поддерживает ресурс. Он редко используется напрямую, обычно вызывается браузером автоматически. Поддерживается не всеми сайтами/ресурсами. Пример:

HTTP-ответ выглядит аналогично:

import requests
response = requests.options('https://httpbin.org')
print(response.text)  # будет пустым
print(response.headers['Allow'])  # 'HEAD, GET, OPTIONS'

GET

GET — самый распространённый HTTP-метод. Его используют для чтения интернет-ресурса. Браузер отправляет метод GET, когда мы открываем какой-либо сайт. Пример:

import requests
response = requests.get('https://httpbin.org/get')
print(response.text)

POST

Метод POST используют для отправки на сервер данных, которые передаются в теле запроса. Для этого при вызове requests.post() надо указать аргумент data, который принимает на вход словарь, список кортежей, байты или файл.

Если для передачи данных используется формат JSON, вместо data можно указать json. Это просто удобная конвенция, которая правильно формирует отправляемый запрос. Пример:

import requests
data_response = requests.post('https://httpbin.org/post', data={'foo': 'bar'})
print(data_response.text)  # переданные данные находятся по ключу form
json_response = requests.post('https://httpbin.org/post', json={'foo': 'bar'})
print(data_response.text)  # ключ form пустой, теперь данные лежат в json

HEAD

Этот метод очень похож на GET — с той лишь разницей, что HEAD возвращает пустое тело ответа. Он нужен, когда нужно посмотреть только на заголовки, не загружая ответ целиком.

Например, мы хотим иметь свежую версию PDF-файла с расписанием автобусов. Файл хранится на каком-то сайте и периодически обновляется. Вместо того, чтобы каждый раз скачивать и сверять файл вручную, можно использовать метод HEAD. Он поможет быстро проверить дату изменения файла по заголовкам ответа.

import requests
response = requests.get('https://httpbin.org/head')
print(response.text)  # ответ будет пустым
print(response.headers)

PUT

Метод PUT очень похож на POST — с той разницей, что несколько последовательных вызовов PUT должны приводить к одному и тому же результату.

POST этого не гарантирует и может привести к неожиданным результатам, например к дублированию созданной сущности.

import requests
response = requests.put('https://httpbin.org/put', data={'foo': 'bar'})
print(response.text)

PATCH

PATCH аналогичен методу POST, но с двумя отличиями: он используется для частичных изменений ресурса и его нельзя использовать в HTML-формах.

В теле запроса передается набор модификаций, которые надо применить.

import requests
response = requests.patch('https://httpbin.org/patch', data={'foo': 'bar'})
print(response.text)

DELETE

Метод используется для удаления ресурса. Поддерживает передачу данных, однако не требует её: тело запроса может быть пустым.

Как и PUT, последовательный вызов DELETE должен приводить к одному и тому же результату.

import requests
response = requests.delete('https://httpbin.org/delete')
print(response.text)

HTTP-коды состояний

Каждый ответ HTTP-запроса обязательно имеет код состояния — трехзначное число, которое как-то характеризует полученный результат. По этому коду можно понять, всё ли успешно отработало, и если произошла ошибка, то почему.

Всего выделяют пять групп кодов состояний:

1хх-коды.

К этой группе относятся информационные коды состояний. Они сообщают клиенту о промежуточном статусе запроса и не являются финальным результатом.

Их немного, и останавливаться на них мы не будем, потому что они встречаются нечасто.

2хх-коды.

Коды из этой группы означают, что запрос принят и обработан сервером без ошибок:

  • 200 OK — запрос выполнен успешно. Чаще всего встречается именно это число.
  • 201 Created — в результате запроса был создан новый ресурс. Как правило, этим кодом отвечают на POST- и иногда PUT-запросы.
  • 202 Accepted — запрос принят, но ещё не выполнен. Используется, когда по какой-то причине сервер не может выполнить его сразу. Например, если обработку делает какой-то сторонний процесс, который выполняется раз в день.
  • 204 No Content — указывает, что тело ответа пустое, но заголовки могут содержать полезную информацию. Не используется с методом HEAD, поскольку ответ на него всегда должен быть пустым.

3хх-коды.

Это группа кодов перенаправления. Это значит, что клиенту нужно сделать какое-то действие, чтобы запрос продолжил выполняться:

  • 301 Moved Permanently — URL запрашиваемого ресурса изменился, новый URL содержится в ответе.
  • 302 Found — аналогичен предыдущему коду. Отличие в том, что URL изменился временно. При этом статусе состояния поисковые системы не будут менять ссылку в своей поисковой выдаче на новую.
  • 304 Not Modified — означает, что содержимое ресурса было закешировано, его содержимое не поменялось и запрос можно не продолжать.

4хх-коды.

Это коды ошибок, которые допустил клиент при формировании запроса:

  • 400 Bad Request — запрос сформирован с ошибкой, поэтому сервер не может его обработать. Причин может быть много, но чаще всего ошибку надо искать в теле запроса.
  • 401 Unauthorized — для продолжения необходимо залогиниться.
  • 403 Forbidden — пользователь залогинен, но у него нет прав для доступа к ресурсу.
  • 404 Not Found — всем известный код: страница не найдена. Некоторые сайты могут возвращать 404 вместо 403, чтобы скрыть информацию от неавторизованных пользователей.
  • 405 Method Not Allowed — данный ресурс не поддерживает метод запроса. Например, так бывает, если разработчик хочет отправить PUT-запрос на ресурс, который его не поддерживает.
  • 429 Too Many Requests — означает, что сработал защитный механизм: он ограничивает слишком частые запросы от одного пользователя. Таким образом защищаются от DDoS- или brute-force-атак.

5хх-коды.

Это ошибки, которые возникли на сервере во время выполнения запроса:

  • 500 Internal Server Error — на сервере произошла неожиданная ошибка. Как правило, происходит из-за того, что в коде сервера возникает исключение.
  • 502 Bad Gateway — возникает, если на сервере используется обратный прокси, который не смог достучаться до приложения.
  • 503 Service Unavailable — сервер пока не готов обработать запрос. В ответе также может содержаться информация о том, когда сервис станет доступен.
  • 504 Gateway Timeout — эта ошибка означает, что обратный прокси не смог получить ответ за отведенное время (обычно — 60 секунд).

Заголовки, текст ответа и файлы Cookie

Теперь рассмотрим, как работать с запросами и ответами в Requests. Чтобы увидеть результат HTTP-запроса, можно использовать один из трех способов.

Выбор способа зависит от того, какие данные мы получили. В непонятной ситуации можно использовать атрибут text, который возвращает содержимое в виде строки:

import requests
response = requests.get('https://httpbin.org/get')
print(response.text)

Если заранее известно, что ответ будет в формате JSON, можно использовать одноименный атрибут, который автоматически распарсит ответ и вернет его в виде словаря:

json_response = response.json()
print(json_response)

Обратите внимание, как изменится вывод функции print().

Наконец, если ответом на запрос является файл, стоит использовать атрибут content, который возвращает байты:

import requests
response = requests.get('https://httpbin.org/image/jpeg')
print(response.content)

Попробуйте вывести на экран response.text для предыдущего запроса и сравните результат.

Заголовок — это дополнительная информация, которой обмениваются клиент и сервер. В заголовках могут содержаться: размер ответа (Content-Length), формат передаваемых данных (Content-Type) или информация о клиенте (User-Agent).

Полный список очень длинный, знать их все необязательно, а часть и вовсе подставляется автоматом. Например, модуль Requests зачастую сам проставляет Content-Type — формат передаваемых данных.

Заголовок состоит из названия и значения, которые разделяются двоеточием, поэтому удобнее всего передавать их в виде словаря. Рассмотрим на примере, как это работает:

import requests
response = requests.get('https://httpbin.org/image', headers={'Accept': 'image/jpeg'})
print(response.headers)

Здесь мы передали заголовок, который указывает, в каком формате мы хотим получить изображение. Попробуйте поменять значение на image/png и посмотрите, как изменится ответ.

Так же можно посмотреть и на заголовки запроса:

print(response.request.headers)

Обратите внимание, что Requests сам подставил информацию о клиенте — User-Agent.

Cookie (куки) — это информация, которую сервер отправляет браузеру для хранения. Они позволяют зафиксировать некоторое состояние. Например, в куки может храниться информация о том, что пользователь уже залогинен. Она хранится в браузере и передается на сервер при каждом запросе, поэтому нам не нужно каждый раз проходить авторизацию заново.

Работать с куками в модуле Requests очень просто:

import requests
response = requests.get('https://httpbin.org/cookies', cookies={'foo': 'bar'})
print(response.text)

Посмотреть, какие куки пришли от сервера, можно при помощи атрибута cookies объекта Response:

print(response.cookies)

Как отправлять запросы при помощи Python Requests

Рассмотрим несколько частых примеров использования модуля Requests, чтобы понять, как отправлять запросы.

Скачивание файлов

import requests
response = requests.get('https://www.python.org/static/img/python-logo.png')
with open('python_logo.png', 'wb') as image:
    image.write(response.content)

Выше описан не самый эффективный способ скачивания файлов. Если файл окажется большого размера, код выше загрузит результат целиком в оперативную память. В лучшем случае программа упадет с ошибкой, в худшем — всё намертво зависнет.

Вот как это можно исправить:

import requests
response = requests.get('https://www.python.org/static/img/python-logo@2x.png', stream=True)

with open('python_logo.png', 'wb') as image:
    for chunk in response.iter_content(chunk_size=1024):
        image.write(chunk)

В этом варианте мы используем параметр stream=True, который открывает соединение, но не скачивает содержимое. Затем мы задаем размер чанка — кусочка информации, который будет скачиваться за одну итерацию цикла, и делаем его равным 1 Кб (1024 байт). Модуль Requests сам закрывает соединение после прочтения последнего чанка.

Чтобы заранее узнать размер файла, можно воспользоваться методом HEAD. Эта информация передается в заголовке ‘Content-Length’ и исчисляется в байтах.

import requests
head_response = requests.head('https://www.python.org/static/img/python-logo@2x.png')
image_size = int(head_response.headers['Content-Length'])
print('Размер загружаемого файла: {0} кб'.format(image_size / 1024))

Авторизация на сайте

Рассмотрим два способа авторизации, которые встречаются чаще всего: Basic Auth и Bearer Auth. В обоих случаях механизм очень похожий — запрос должен передать заголовок ‘Authorization’ с каким-то значением. Для Basic Auth — это логин и пароль, закодированные в base64, для Bearer — токен, который мы получили на сайте заранее.

Для базовой авторизации у модуля Requests есть очень удобный параметр auth=, который делает всю работу за нас:

import requests
response = requests.get('https://httpbin.org/basic-auth/foo/bar')
print(response.status_code)  # 401
response = requests.get('https://httpbin.org/basic-auth/foo/bar', auth=('foo', 'bar'))
print(response.status_code)  # 200
print(response.request.headers[‘Authorization’])  # 'Basic Zm9vOmJhcg=='

Обратите внимание, что модуль Requests сам добавил заголовок Authorization и подставил туда закодированные логин и пароль.

Для Bearer Auth нам придется добавлять его самостоятельно:

import requests
response = requests.get('https://httpbin.org/bearer')
print(response.status_code)  # 401
headers = {'Authorization': 'Bearer some_token'}
response = requests.get('https://httpbin.org/bearer', headers=headers)
print(response.status_code)  # 200

У каждого API своя спецификация — вместо Bearer может быть Token или что-то другое. Поэтому важно внимательно читать документацию сервиса.

Мультискачивание

Напишем код, который умеет скачивать сразу несколько файлов. Для этого вынесем работу с модулем Requests в отдельную функцию и параметризируем место сохранения файла.

Не забывайте про сохранение файла по чанкам, чтобы крупные файлы не загружались в память целиком.

import requests
def download_file(url, save_path):
    response = requests.get(url, stream=True)
    with open(save_path, 'wb') as file:
        for chunk in response.iter_content(chunk_size=1024):
            file.write(chunk)
download_list = [
    'https://cdn.pixabay.com/photo/2022/04/10/19/33/house-7124141_1280.jpg',
    'https://cdn.pixabay.com/photo/2022/08/05/18/50/houseplant-7367379_1280.jpg',
    'https://cdn.pixabay.com/photo/2022/06/09/04/53/ride-7251713_1280.png',
]

for url in download_list:
    save_path = url.split('/')[-1]
    download_file(url, save_path)

Заключение

Модуль Requests — мощный инструмент, с которым разработчик может сделать сложный HTTP-запрос всего в пару строк. У него интуитивно понятный интерфейс, поэтому он так популярен в сообществе Python.

С помощью модуля реквест можно выполнить множество функций: от авторизации на сайте до скачивания нескольких файлов одновременно.

This part of the documentation covers all the interfaces of Requests. For
parts where Requests depends on external libraries, we document the most
important right here and provide links to the canonical documentation.

Main Interface¶

All of Requests’ functionality can be accessed by these 7 methods.
They all return an instance of the Response object.

requests.request(method, url, **kwargs)[source]

Constructs and sends a Request.

Parameters:
  • method – method for the new Request object.
  • url – URL for the new Request object.
  • params – (optional) Dictionary, list of tuples or bytes to send
    in the body of the Request.
  • data – (optional) Dictionary, list of tuples, bytes, or file-like
    object to send in the body of the Request.
  • json – (optional) A JSON serializable Python object to send in the body of the Request.
  • headers – (optional) Dictionary of HTTP Headers to send with the Request.
  • cookies – (optional) Dict or CookieJar object to send with the Request.
  • files – (optional) Dictionary of 'name': file-like-objects (or {'name': file-tuple}) for multipart encoding upload.
    file-tuple can be a 2-tuple ('filename', fileobj), 3-tuple ('filename', fileobj, 'content_type')
    or a 4-tuple ('filename', fileobj, 'content_type', custom_headers), where 'content-type' is a string
    defining the content type of the given file and custom_headers a dict-like object containing additional headers
    to add for the file.
  • auth – (optional) Auth tuple to enable Basic/Digest/Custom HTTP Auth.
  • timeout (float or tuple) – (optional) How many seconds to wait for the server to send data
    before giving up, as a float, or a (connect timeout, read
    timeout)
    tuple.
  • allow_redirects (bool) – (optional) Boolean. Enable/disable GET/OPTIONS/POST/PUT/PATCH/DELETE/HEAD redirection. Defaults to True.
  • proxies – (optional) Dictionary mapping protocol to the URL of the proxy.
  • verify – (optional) Either a boolean, in which case it controls whether we verify
    the server’s TLS certificate, or a string, in which case it must be a path
    to a CA bundle to use. Defaults to True.
  • stream – (optional) if False, the response content will be immediately downloaded.
  • cert – (optional) if String, path to ssl client cert file (.pem). If Tuple, (‘cert’, ‘key’) pair.
Returns:

Response object

Return type:

requests.Response

Usage:

>>> import requests
>>> req = requests.request('GET', 'https://httpbin.org/get')
<Response [200]>
requests.head(url, **kwargs)[source]

Sends a HEAD request.

Parameters:
  • url – URL for the new Request object.
  • **kwargs – Optional arguments that request takes.
Returns:

Response object

Return type:

requests.Response

requests.get(url, params=None, **kwargs)[source]

Sends a GET request.

Parameters:
  • url – URL for the new Request object.
  • params – (optional) Dictionary, list of tuples or bytes to send
    in the body of the Request.
  • **kwargs – Optional arguments that request takes.
Returns:

Response object

Return type:

requests.Response

requests.post(url, data=None, json=None, **kwargs)[source]

Sends a POST request.

Parameters:
  • url – URL for the new Request object.
  • data – (optional) Dictionary, list of tuples, bytes, or file-like
    object to send in the body of the Request.
  • json – (optional) json data to send in the body of the Request.
  • **kwargs – Optional arguments that request takes.
Returns:

Response object

Return type:

requests.Response

requests.put(url, data=None, **kwargs)[source]

Sends a PUT request.

Parameters:
  • url – URL for the new Request object.
  • data – (optional) Dictionary, list of tuples, bytes, or file-like
    object to send in the body of the Request.
  • json – (optional) json data to send in the body of the Request.
  • **kwargs – Optional arguments that request takes.
Returns:

Response object

Return type:

requests.Response

requests.patch(url, data=None, **kwargs)[source]

Sends a PATCH request.

Parameters:
  • url – URL for the new Request object.
  • data – (optional) Dictionary, list of tuples, bytes, or file-like
    object to send in the body of the Request.
  • json – (optional) json data to send in the body of the Request.
  • **kwargs – Optional arguments that request takes.
Returns:

Response object

Return type:

requests.Response

requests.delete(url, **kwargs)[source]

Sends a DELETE request.

Parameters:
  • url – URL for the new Request object.
  • **kwargs – Optional arguments that request takes.
Returns:

Response object

Return type:

requests.Response

Exceptions¶

exception requests.RequestException(*args, **kwargs)[source]

There was an ambiguous exception that occurred while handling your
request.

exception requests.ConnectionError(*args, **kwargs)[source]

A Connection error occurred.

exception requests.HTTPError(*args, **kwargs)[source]

An HTTP error occurred.

exception requests.URLRequired(*args, **kwargs)[source]

A valid URL is required to make a request.

exception requests.TooManyRedirects(*args, **kwargs)[source]

Too many redirects.

exception requests.ConnectTimeout(*args, **kwargs)[source]

The request timed out while trying to connect to the remote server.

Requests that produced this error are safe to retry.

exception requests.ReadTimeout(*args, **kwargs)[source]

The server did not send any data in the allotted amount of time.

exception requests.Timeout(*args, **kwargs)[source]

The request timed out.

Catching this error will catch both
ConnectTimeout and
ReadTimeout errors.

Request Sessions¶

class requests.Session[source]

A Requests session.

Provides cookie persistence, connection-pooling, and configuration.

Basic Usage:

>>> import requests
>>> s = requests.Session()
>>> s.get('https://httpbin.org/get')
<Response [200]>

Or as a context manager:

>>> with requests.Session() as s:
>>>     s.get('https://httpbin.org/get')
<Response [200]>
auth = None

Default Authentication tuple or object to attach to
Request.

cert = None

SSL client certificate default, if String, path to ssl client
cert file (.pem). If Tuple, (‘cert’, ‘key’) pair.

close()[source]

Closes all adapters and as such the session

cookies = None

A CookieJar containing all currently outstanding cookies set on this
session. By default it is a
RequestsCookieJar, but
may be any other cookielib.CookieJar compatible object.

delete(url, **kwargs)[source]

Sends a DELETE request. Returns Response object.

Parameters:
  • url – URL for the new Request object.
  • **kwargs – Optional arguments that request takes.
Return type:

requests.Response

get(url, **kwargs)[source]

Sends a GET request. Returns Response object.

Parameters:
  • url – URL for the new Request object.
  • **kwargs – Optional arguments that request takes.
Return type:

requests.Response

get_adapter(url)[source]

Returns the appropriate connection adapter for the given URL.

Return type: requests.adapters.BaseAdapter
get_redirect_target(resp)

Receives a Response. Returns a redirect URI or None

head(url, **kwargs)[source]

Sends a HEAD request. Returns Response object.

Parameters:
  • url – URL for the new Request object.
  • **kwargs – Optional arguments that request takes.
Return type:

requests.Response

A case-insensitive dictionary of headers to be sent on each
Request sent from this
Session.

hooks = None

Event-handling hooks.

max_redirects = None

Maximum number of redirects allowed. If the request exceeds this
limit, a TooManyRedirects exception is raised.
This defaults to requests.models.DEFAULT_REDIRECT_LIMIT, which is
30.

merge_environment_settings(url, proxies, stream, verify, cert)[source]

Check the environment and merge it with some settings.

Return type: dict
mount(prefix, adapter)[source]

Registers a connection adapter to a prefix.

Adapters are sorted in descending order by prefix length.

options(url, **kwargs)[source]

Sends a OPTIONS request. Returns Response object.

Parameters:
  • url – URL for the new Request object.
  • **kwargs – Optional arguments that request takes.
Return type:

requests.Response

params = None

Dictionary of querystring data to attach to each
Request. The dictionary values may be lists for
representing multivalued query parameters.

patch(url, data=None, **kwargs)[source]

Sends a PATCH request. Returns Response object.

Parameters:
  • url – URL for the new Request object.
  • data – (optional) Dictionary, list of tuples, bytes, or file-like
    object to send in the body of the Request.
  • **kwargs – Optional arguments that request takes.
Return type:

requests.Response

post(url, data=None, json=None, **kwargs)[source]

Sends a POST request. Returns Response object.

Parameters:
  • url – URL for the new Request object.
  • data – (optional) Dictionary, list of tuples, bytes, or file-like
    object to send in the body of the Request.
  • json – (optional) json to send in the body of the Request.
  • **kwargs – Optional arguments that request takes.
Return type:

requests.Response

prepare_request(request)[source]

Constructs a PreparedRequest for
transmission and returns it. The PreparedRequest has settings
merged from the Request instance and those of the
Session.

Parameters: requestRequest instance to prepare with this
session’s settings.
Return type: requests.PreparedRequest
proxies = None

Dictionary mapping protocol or protocol and host to the URL of the proxy
(e.g. {‘http’: ‘foo.bar:3128’, ‘http://host.name’: ‘foo.bar:4012’}) to
be used on each Request.

put(url, data=None, **kwargs)[source]

Sends a PUT request. Returns Response object.

Parameters:
  • url – URL for the new Request object.
  • data – (optional) Dictionary, list of tuples, bytes, or file-like
    object to send in the body of the Request.
  • **kwargs – Optional arguments that request takes.
Return type:

requests.Response

rebuild_auth(prepared_request, response)

When being redirected we may want to strip authentication from the
request to avoid leaking credentials. This method intelligently removes
and reapplies authentication where possible to avoid credential loss.

rebuild_method(prepared_request, response)

When being redirected we may want to change the method of the request
based on certain specs or browser behavior.

rebuild_proxies(prepared_request, proxies)

This method re-evaluates the proxy configuration by considering the
environment variables. If we are redirected to a URL covered by
NO_PROXY, we strip the proxy configuration. Otherwise, we set missing
proxy keys for this URL (in case they were stripped by a previous
redirect).

This method also replaces the Proxy-Authorization header where
necessary.

Return type: dict
request(method, url, params=None, data=None, headers=None, cookies=None, files=None, auth=None, timeout=None, allow_redirects=True, proxies=None, hooks=None, stream=None, verify=None, cert=None, json=None)[source]

Constructs a Request, prepares it and sends it.
Returns Response object.

Parameters:
  • method – method for the new Request object.
  • url – URL for the new Request object.
  • params – (optional) Dictionary or bytes to be sent in the query
    string for the Request.
  • data – (optional) Dictionary, list of tuples, bytes, or file-like
    object to send in the body of the Request.
  • json – (optional) json to send in the body of the
    Request.
  • headers – (optional) Dictionary of HTTP Headers to send with the
    Request.
  • cookies – (optional) Dict or CookieJar object to send with the
    Request.
  • files – (optional) Dictionary of 'filename': file-like-objects
    for multipart encoding upload.
  • auth – (optional) Auth tuple or callable to enable
    Basic/Digest/Custom HTTP Auth.
  • timeout (float or tuple) – (optional) How long to wait for the server to send
    data before giving up, as a float, or a (connect timeout,
    read timeout)
    tuple.
  • allow_redirects (bool) – (optional) Set to True by default.
  • proxies – (optional) Dictionary mapping protocol or protocol and
    hostname to the URL of the proxy.
  • stream – (optional) whether to immediately download the response
    content. Defaults to False.
  • verify – (optional) Either a boolean, in which case it controls whether we verify
    the server’s TLS certificate, or a string, in which case it must be a path
    to a CA bundle to use. Defaults to True.
  • cert – (optional) if String, path to ssl client cert file (.pem).
    If Tuple, (‘cert’, ‘key’) pair.
Return type:

requests.Response

resolve_redirects(resp, req, stream=False, timeout=None, verify=True, cert=None, proxies=None, yield_requests=False, **adapter_kwargs)

Receives a Response. Returns a generator of Responses or Requests.

send(request, **kwargs)[source]

Send a given PreparedRequest.

Return type: requests.Response
should_strip_auth(old_url, new_url)

Decide whether Authorization header should be removed when redirecting

stream = None

Stream response content default.

trust_env = None

Trust environment settings for proxy configuration, default
authentication and similar.

verify = None

SSL Verification default.

Lower-Level Classes¶

class requests.Request(method=None, url=None, headers=None, files=None, data=None, params=None, auth=None, cookies=None, hooks=None, json=None)[source]

A user-created Request object.

Used to prepare a PreparedRequest, which is sent to the server.

Parameters:
  • method – HTTP method to use.
  • url – URL to send.
  • headers – dictionary of headers to send.
  • files – dictionary of {filename: fileobject} files to multipart upload.
  • data – the body to attach to the request. If a dictionary or
    list of tuples [(key, value)] is provided, form-encoding will
    take place.
  • json – json for the body to attach to the request (if files or data is not specified).
  • params – URL parameters to append to the URL. If a dictionary or
    list of tuples [(key, value)] is provided, form-encoding will
    take place.
  • auth – Auth handler or (user, pass) tuple.
  • cookies – dictionary or CookieJar of cookies to attach to this request.
  • hooks – dictionary of callback hooks, for internal usage.

Usage:

>>> import requests
>>> req = requests.Request('GET', 'https://httpbin.org/get')
>>> req.prepare()
<PreparedRequest [GET]>
deregister_hook(event, hook)

Deregister a previously registered hook.
Returns True if the hook existed, False if not.

prepare()[source]

Constructs a PreparedRequest for transmission and returns it.

register_hook(event, hook)

Properly register a hook.

class requests.Response[source]

The Response object, which contains a
server’s response to an HTTP request.

apparent_encoding

The apparent encoding, provided by the chardet library.

close()[source]

Releases the connection back to the pool. Once this method has been
called the underlying raw object must not be accessed again.

Note: Should not normally need to be called explicitly.

content

Content of the response, in bytes.

cookies = None

A CookieJar of Cookies the server sent back.

elapsed = None

The amount of time elapsed between sending the request
and the arrival of the response (as a timedelta).
This property specifically measures the time taken between sending
the first byte of the request and finishing parsing the headers. It
is therefore unaffected by consuming the response content or the
value of the stream keyword argument.

encoding = None

Encoding to decode with when accessing r.text.

Case-insensitive Dictionary of Response Headers.
For example, headers['content-encoding'] will return the
value of a 'Content-Encoding' response header.

history = None

A list of Response objects from
the history of the Request. Any redirect responses will end
up here. The list is sorted from the oldest to the most recent request.

is_permanent_redirect

True if this Response one of the permanent versions of redirect.

is_redirect

True if this Response is a well-formed HTTP redirect that could have
been processed automatically (by Session.resolve_redirects).

iter_content(chunk_size=1, decode_unicode=False)[source]

Iterates over the response data. When stream=True is set on the
request, this avoids reading the content at once into memory for
large responses. The chunk size is the number of bytes it should
read into memory. This is not necessarily the length of each item
returned as decoding can take place.

chunk_size must be of type int or None. A value of None will
function differently depending on the value of stream.
stream=True will read data as it arrives in whatever size the
chunks are received. If stream=False, data is returned as
a single chunk.

If decode_unicode is True, content will be decoded using the best
available encoding based on the response.

iter_lines(chunk_size=512, decode_unicode=False, delimiter=None)[source]

Iterates over the response data, one line at a time. When
stream=True is set on the request, this avoids reading the
content at once into memory for large responses.

Note

This method is not reentrant safe.

json(**kwargs)[source]

Returns the json-encoded content of a response, if any.

Parameters: **kwargs – Optional arguments that json.loads takes.
Raises: ValueError – If the response body does not contain valid json.
links

Returns the parsed header links of the response, if any.

next

Returns a PreparedRequest for the next request in a redirect chain, if there is one.

ok

Returns True if status_code is less than 400, False if not.

This attribute checks if the status code of the response is between
400 and 600 to see if there was a client error or a server error. If
the status code is between 200 and 400, this will return True. This
is not a check to see if the response code is 200 OK.

raise_for_status()[source]

Raises stored HTTPError, if one occurred.

reason = None

Textual reason of responded HTTP Status, e.g. “Not Found” or “OK”.

request = None

The PreparedRequest object to which this
is a response.

status_code = None

Integer Code of responded HTTP Status, e.g. 404 or 200.

text

Content of the response, in unicode.

If Response.encoding is None, encoding will be guessed using
chardet.

The encoding of the response content is determined based solely on HTTP
headers, following RFC 2616 to the letter. If you can take advantage of
non-HTTP knowledge to make a better guess at the encoding, you should
set r.encoding appropriately before accessing this property.

url = None

Final URL location of Response.

Lower-Lower-Level Classes¶

class requests.PreparedRequest[source]

The fully mutable PreparedRequest object,
containing the exact bytes that will be sent to the server.

Generated from either a Request object or manually.

Usage:

>>> import requests
>>> req = requests.Request('GET', 'https://httpbin.org/get')
>>> r = req.prepare()
<PreparedRequest [GET]>

>>> s = requests.Session()
>>> s.send(r)
<Response [200]>
body = None

request body to send to the server.

deregister_hook(event, hook)

Deregister a previously registered hook.
Returns True if the hook existed, False if not.

dictionary of HTTP headers.

hooks = None

dictionary of callback hooks, for internal usage.

method = None

HTTP verb to send to the server.

path_url

Build the path URL to use.

prepare(method=None, url=None, headers=None, files=None, data=None, params=None, auth=None, cookies=None, hooks=None, json=None)[source]

Prepares the entire request with the given parameters.

prepare_auth(auth, url=»)[source]

Prepares the given HTTP auth data.

prepare_body(data, files, json=None)[source]

Prepares the given HTTP body data.

prepare_content_length(body)[source]

Prepare Content-Length header based on request method and body

prepare_cookies(cookies)[source]

Prepares the given HTTP cookie data.

This function eventually generates a Cookie header from the
given cookies using cookielib. Due to cookielib’s design, the header
will not be regenerated if it already exists, meaning this function
can only be called once for the life of the
PreparedRequest object. Any subsequent calls
to prepare_cookies will have no actual effect, unless the “Cookie”
header is removed beforehand.

Prepares the given HTTP headers.

prepare_hooks(hooks)[source]

Prepares the given hooks.

prepare_method(method)[source]

Prepares the given HTTP method.

prepare_url(url, params)[source]

Prepares the given HTTP URL.

register_hook(event, hook)

Properly register a hook.

url = None

HTTP URL to send the request to.

class requests.adapters.BaseAdapter[source]

The Base Transport Adapter

close()[source]

Cleans up adapter specific items.

send(request, stream=False, timeout=None, verify=True, cert=None, proxies=None)[source]

Sends PreparedRequest object. Returns Response object.

Parameters:
  • request – The PreparedRequest being sent.
  • stream – (optional) Whether to stream the request content.
  • timeout (float or tuple) – (optional) How long to wait for the server to send
    data before giving up, as a float, or a (connect timeout,
    read timeout)
    tuple.
  • verify – (optional) Either a boolean, in which case it controls whether we verify
    the server’s TLS certificate, or a string, in which case it must be a path
    to a CA bundle to use
  • cert – (optional) Any user-provided SSL certificate to be trusted.
  • proxies – (optional) The proxies dictionary to apply to the request.
class requests.adapters.HTTPAdapter(pool_connections=10, pool_maxsize=10, max_retries=0, pool_block=False)[source]

The built-in HTTP Adapter for urllib3.

Provides a general-case interface for Requests sessions to contact HTTP and
HTTPS urls by implementing the Transport Adapter interface. This class will
usually be created by the Session class under the
covers.

Parameters:
  • pool_connections – The number of urllib3 connection pools to cache.
  • pool_maxsize – The maximum number of connections to save in the pool.
  • max_retries – The maximum number of retries each connection
    should attempt. Note, this applies only to failed DNS lookups, socket
    connections and connection timeouts, never to requests where data has
    made it to the server. By default, Requests does not retry failed
    connections. If you need granular control over the conditions under
    which we retry a request, import urllib3’s Retry class and pass
    that instead.
  • pool_block – Whether the connection pool should block for connections.

Usage:

>>> import requests
>>> s = requests.Session()
>>> a = requests.adapters.HTTPAdapter(max_retries=3)
>>> s.mount('http://', a)

Add any headers needed by the connection. As of v2.0 this does
nothing by default, but is left for overriding by users that subclass
the HTTPAdapter.

This should not be called from user code, and is only exposed for use
when subclassing the
HTTPAdapter.

Parameters:
  • request – The PreparedRequest to add headers to.
  • kwargs – The keyword arguments from the call to send().
build_response(req, resp)[source]

Builds a Response object from a urllib3
response. This should not be called from user code, and is only exposed
for use when subclassing the
HTTPAdapter

Parameters:
  • req – The PreparedRequest used to generate the response.
  • resp – The urllib3 response object.
Return type:

requests.Response

cert_verify(conn, url, verify, cert)[source]

Verify a SSL certificate. This method should not be called from user
code, and is only exposed for use when subclassing the
HTTPAdapter.

Parameters:
  • conn – The urllib3 connection object associated with the cert.
  • url – The requested URL.
  • verify – Either a boolean, in which case it controls whether we verify
    the server’s TLS certificate, or a string, in which case it must be a path
    to a CA bundle to use
  • cert – The SSL certificate to verify.
close()[source]

Disposes of any internal state.

Currently, this closes the PoolManager and any active ProxyManager,
which closes any pooled connections.

get_connection(url, proxies=None)[source]

Returns a urllib3 connection for the given URL. This should not be
called from user code, and is only exposed for use when subclassing the
HTTPAdapter.

Parameters:
  • url – The URL to connect to.
  • proxies – (optional) A Requests-style dictionary of proxies used on this request.
Return type:

urllib3.ConnectionPool

init_poolmanager(connections, maxsize, block=False, **pool_kwargs)[source]

Initializes a urllib3 PoolManager.

This method should not be called from user code, and is only
exposed for use when subclassing the
HTTPAdapter.

Parameters:
  • connections – The number of urllib3 connection pools to cache.
  • maxsize – The maximum number of connections to save in the pool.
  • block – Block when no free connections are available.
  • pool_kwargs – Extra keyword arguments used to initialize the Pool Manager.

Returns a dictionary of the headers to add to any request sent
through a proxy. This works with urllib3 magic to ensure that they are
correctly sent to the proxy, rather than in a tunnelled request if
CONNECT is being used.

This should not be called from user code, and is only exposed for use
when subclassing the
HTTPAdapter.

Parameters: proxy – The url of the proxy being used for this request.
Return type: dict
proxy_manager_for(proxy, **proxy_kwargs)[source]

Return urllib3 ProxyManager for the given proxy.

This method should not be called from user code, and is only
exposed for use when subclassing the
HTTPAdapter.

Parameters:
  • proxy – The proxy to return a urllib3 ProxyManager for.
  • proxy_kwargs – Extra keyword arguments used to configure the Proxy Manager.
Returns:

ProxyManager

Return type:

urllib3.ProxyManager

request_url(request, proxies)[source]

Obtain the url to use when making the final request.

If the message is being sent through a HTTP proxy, the full URL has to
be used. Otherwise, we should only use the path portion of the URL.

This should not be called from user code, and is only exposed for use
when subclassing the
HTTPAdapter.

Parameters:
  • request – The PreparedRequest being sent.
  • proxies – A dictionary of schemes or schemes and hosts to proxy URLs.
Return type:

str

send(request, stream=False, timeout=None, verify=True, cert=None, proxies=None)[source]

Sends PreparedRequest object. Returns Response object.

Parameters:
  • request – The PreparedRequest being sent.
  • stream – (optional) Whether to stream the request content.
  • timeout (float or tuple or urllib3 Timeout object) – (optional) How long to wait for the server to send
    data before giving up, as a float, or a (connect timeout,
    read timeout)
    tuple.
  • verify – (optional) Either a boolean, in which case it controls whether
    we verify the server’s TLS certificate, or a string, in which case it
    must be a path to a CA bundle to use
  • cert – (optional) Any user-provided SSL certificate to be trusted.
  • proxies – (optional) The proxies dictionary to apply to the request.
Return type:

requests.Response

Authentication¶

class requests.auth.AuthBase[source]

Base class that all auth implementations derive from

class requests.auth.HTTPBasicAuth(username, password)[source]

Attaches HTTP Basic Authentication to the given Request object.

class requests.auth.HTTPDigestAuth(username, password)[source]

Attaches HTTP Digest Authentication to the given Request object.

Encodings¶

requests.utils.get_encodings_from_content(content)[source]

Returns encodings from given content string.

Parameters: content – bytestring to extract encodings from.

Returns encodings from given HTTP Header Dict.

Parameters: headers – dictionary to extract encoding from.
Return type: str
requests.utils.get_unicode_from_response(r)[source]

Returns the requested content back in unicode.

Parameters: r – Response object to get unicode content from.

Tried:

  1. charset from content-type
  2. fall back and replace all unicode characters
Return type: str

Cookies¶

requests.utils.dict_from_cookiejar(cj)[source]

Returns a key/value dictionary from a CookieJar.

Parameters: cj – CookieJar object to extract cookies from.
Return type: dict
requests.utils.add_dict_to_cookiejar(cj, cookie_dict)[source]

Returns a CookieJar from a key/value dictionary.

Parameters:
  • cj – CookieJar to insert cookies into.
  • cookie_dict – Dict of key/values to insert into CookieJar.
Return type:

CookieJar

requests.cookies.cookiejar_from_dict(cookie_dict, cookiejar=None, overwrite=True)[source]

Returns a CookieJar from a key/value dictionary.

Parameters:
  • cookie_dict – Dict of key/values to insert into CookieJar.
  • cookiejar – (optional) A cookiejar to add the cookies to.
  • overwrite – (optional) If False, will not replace cookies
    already in the jar with new ones.
Return type:

CookieJar

class requests.cookies.RequestsCookieJar(policy=None)[source]

Compatibility class; is a cookielib.CookieJar, but exposes a dict
interface.

This is the CookieJar we create by default for requests and sessions that
don’t specify one, since some clients may expect response.cookies and
session.cookies to support dict operations.

Requests does not use the dict interface internally; it’s just for
compatibility with external client code. All requests code should work
out of the box with externally provided instances of CookieJar, e.g.
LWPCookieJar and FileCookieJar.

Unlike a regular CookieJar, this class is pickleable.

Warning

dictionary operations that are normally O(1) may be O(n).

Add correct Cookie: header to request (urllib.request.Request object).

The Cookie2 header is also added unless policy.hide_cookie2 is true.

clear(domain=None, path=None, name=None)

Clear some cookies.

Invoking this method without arguments will clear all cookies. If
given a single argument, only cookies belonging to that domain will be
removed. If given two arguments, cookies belonging to the specified
path within that domain are removed. If given three arguments, then
the cookie with the specified name, path and domain is removed.

Raises KeyError if no matching cookie exists.

clear_expired_cookies()

Discard all expired cookies.

You probably don’t need to call this method: expired cookies are never
sent back to the server (provided you’re using DefaultCookiePolicy),
this method is called by CookieJar itself every so often, and the
.save() method won’t save expired cookies anyway (unless you ask
otherwise by passing a true ignore_expires argument).

clear_session_cookies()

Discard all session cookies.

Note that the .save() method won’t save session cookies anyway, unless
you ask otherwise by passing a true ignore_discard argument.

copy()[source]

Return a copy of this RequestsCookieJar.

Extract cookies from response, where allowable given the request.

get(name, default=None, domain=None, path=None)[source]

Dict-like get() that also supports optional domain and path args in
order to resolve naming collisions from using one cookie jar over
multiple domains.

Warning

operation is O(n), not O(1).

get_dict(domain=None, path=None)[source]

Takes as an argument an optional domain and path and returns a plain
old Python dict of name-value pairs of cookies that meet the
requirements.

Return type: dict
get_policy()[source]

Return the CookiePolicy instance used.

items()[source]

Dict-like items() that returns a list of name-value tuples from the
jar. Allows client-code to call dict(RequestsCookieJar) and get a
vanilla python dict of key value pairs.

See also

keys() and values().

iteritems()[source]

Dict-like iteritems() that returns an iterator of name-value tuples
from the jar.

See also

iterkeys() and itervalues().

iterkeys()[source]

Dict-like iterkeys() that returns an iterator of names of cookies
from the jar.

See also

itervalues() and iteritems().

itervalues()[source]

Dict-like itervalues() that returns an iterator of values of cookies
from the jar.

See also

iterkeys() and iteritems().

keys()[source]

Dict-like keys() that returns a list of names of cookies from the
jar.

See also

values() and items().

list_domains()[source]

Utility method to list all the domains in the jar.

list_paths()[source]

Utility method to list all the paths in the jar.

make_cookies(response, request)

Return sequence of Cookie objects extracted from response object.

multiple_domains()[source]

Returns True if there are multiple domains in the jar.
Returns False otherwise.

Return type: bool
pop(k[, d]) → v, remove specified key and return the corresponding value.¶

If key is not found, d is returned if given, otherwise KeyError is raised.

popitem() → (k, v), remove and return some (key, value) pair¶

as a 2-tuple; but raise KeyError if D is empty.

set(name, value, **kwargs)[source]

Dict-like set() that also supports optional domain and path args in
order to resolve naming collisions from using one cookie jar over
multiple domains.

set_cookie(cookie, *args, **kwargs)[source]

Set a cookie, without checking whether or not it should be set.

set_cookie_if_ok(cookie, request)

Set a cookie if policy says it’s OK to do so.

setdefault(k[, d]) → D.get(k,d), also set D[k]=d if k not in D¶
update(other)[source]

Updates this jar with cookies from another CookieJar or dict-like

values()[source]

Dict-like values() that returns a list of values of cookies from the
jar.

See also

keys() and items().

class requests.cookies.CookieConflictError[source]

There are two cookies that meet the criteria specified in the cookie jar.
Use .get and .set and include domain and path args in order to be more specific.

with_traceback()

Exception.with_traceback(tb) –
set self.__traceback__ to tb and return self.

Status Code Lookup¶

requests.codes

The codes object defines a mapping from common names for HTTP statuses
to their numerical codes, accessible either as attributes or as dictionary
items.

>>> requests.codes['temporary_redirect']
307
>>> requests.codes.teapot
418
>>> requests.codes['o/']
200

Some codes have multiple names, and both upper- and lower-case versions of
the names are allowed. For example, codes.ok, codes.OK, and
codes.okay all correspond to the HTTP status code 200.

  • 100: continue
  • 101: switching_protocols
  • 102: processing
  • 103: checkpoint
  • 122: uri_too_long, request_uri_too_long
  • 200: ok, okay, all_ok, all_okay, all_good, o/,
  • 201: created
  • 202: accepted
  • 203: non_authoritative_info, non_authoritative_information
  • 204: no_content
  • 205: reset_content, reset
  • 206: partial_content, partial
  • 207: multi_status, multiple_status, multi_stati, multiple_stati
  • 208: already_reported
  • 226: im_used
  • 300: multiple_choices
  • 301: moved_permanently, moved, o-
  • 302: found
  • 303: see_other, other
  • 304: not_modified
  • 305: use_proxy
  • 306: switch_proxy
  • 307: temporary_redirect, temporary_moved, temporary
  • 308: permanent_redirect, resume_incomplete, resume
  • 400: bad_request, bad
  • 401: unauthorized
  • 402: payment_required, payment
  • 403: forbidden
  • 404: not_found, -o-
  • 405: method_not_allowed, not_allowed
  • 406: not_acceptable
  • 407: proxy_authentication_required, proxy_auth, proxy_authentication
  • 408: request_timeout, timeout
  • 409: conflict
  • 410: gone
  • 411: length_required
  • 412: precondition_failed, precondition
  • 413: request_entity_too_large
  • 414: request_uri_too_large
  • 415: unsupported_media_type, unsupported_media, media_type
  • 416: requested_range_not_satisfiable, requested_range, range_not_satisfiable
  • 417: expectation_failed
  • 418: im_a_teapot, teapot, i_am_a_teapot
  • 421: misdirected_request
  • 422: unprocessable_entity, unprocessable
  • 423: locked
  • 424: failed_dependency, dependency
  • 425: unordered_collection, unordered
  • 426: upgrade_required, upgrade
  • 428: precondition_required, precondition
  • 429: too_many_requests, too_many
  • 431: header_fields_too_large, fields_too_large
  • 444: no_response, none
  • 449: retry_with, retry
  • 450: blocked_by_windows_parental_controls, parental_controls
  • 451: unavailable_for_legal_reasons, legal_reasons
  • 499: client_closed_request
  • 500: internal_server_error, server_error, /o,
  • 501: not_implemented
  • 502: bad_gateway
  • 503: service_unavailable, unavailable
  • 504: gateway_timeout
  • 505: http_version_not_supported, http_version
  • 506: variant_also_negotiates
  • 507: insufficient_storage
  • 509: bandwidth_limit_exceeded, bandwidth
  • 510: not_extended
  • 511: network_authentication_required, network_auth, network_authentication

Migrating to 1.x¶

This section details the main differences between 0.x and 1.x and is meant
to ease the pain of upgrading.

API Changes¶

  • Response.json is now a callable and not a property of a response.

    import requests
    r = requests.get('https://github.com/timeline.json')
    r.json()   # This *call* raises an exception if JSON decoding fails
    
  • The Session API has changed. Sessions objects no longer take parameters.
    Session is also now capitalized, but it can still be
    instantiated with a lowercase session for backwards compatibility.

    s = requests.Session()    # formerly, session took parameters
    s.auth = auth
    s.headers.update(headers)
    r = s.get('https://httpbin.org/headers')
    
  • All request hooks have been removed except ‘response’.

  • Authentication helpers have been broken out into separate modules. See
    requests-oauthlib and requests-kerberos.

  • The parameter for streaming requests was changed from prefetch to
    stream and the logic was inverted. In addition, stream is now
    required for raw response reading.

    # in 0.x, passing prefetch=False would accomplish the same thing
    r = requests.get('https://github.com/timeline.json', stream=True)
    for chunk in r.iter_content(8192):
        ...
    
  • The config parameter to the requests method has been removed. Some of
    these options are now configured on a Session such as keep-alive and
    maximum number of redirects. The verbosity option should be handled by
    configuring logging.

    import requests
    import logging
    
    # Enabling debugging at http.client level (requests->urllib3->http.client)
    # you will see the REQUEST, including HEADERS and DATA, and RESPONSE with HEADERS but without DATA.
    # the only thing missing will be the response.body which is not logged.
    try: # for Python 3
        from http.client import HTTPConnection
    except ImportError:
        from httplib import HTTPConnection
    HTTPConnection.debuglevel = 1
    
    logging.basicConfig() # you need to initialize logging, otherwise you will not see anything from requests
    logging.getLogger().setLevel(logging.DEBUG)
    requests_log = logging.getLogger("urllib3")
    requests_log.setLevel(logging.DEBUG)
    requests_log.propagate = True
    
    requests.get('https://httpbin.org/headers')
    

Licensing¶

One key difference that has nothing to do with the API is a change in the
license from the ISC license to the Apache 2.0 license. The Apache 2.0
license ensures that contributions to Requests are also covered by the Apache
2.0 license.

Migrating to 2.x¶

Compared with the 1.0 release, there were relatively few backwards
incompatible changes, but there are still a few issues to be aware of with
this major release.

For more details on the changes in this release including new APIs, links
to the relevant GitHub issues and some of the bug fixes, read Cory’s blog
on the subject.

API Changes¶

  • There were a couple changes to how Requests handles exceptions.
    RequestException is now a subclass of IOError rather than
    RuntimeError as that more accurately categorizes the type of error.
    In addition, an invalid URL escape sequence now raises a subclass of
    RequestException rather than a ValueError.

    requests.get('http://%zz/')   # raises requests.exceptions.InvalidURL
    

    Lastly, httplib.IncompleteRead exceptions caused by incorrect chunked
    encoding will now raise a Requests ChunkedEncodingError instead.

  • The proxy API has changed slightly. The scheme for a proxy URL is now
    required.

    proxies = {
      "http": "10.10.1.10:3128",    # use http://10.10.1.10:3128 instead
    }
    
    # In requests 1.x, this was legal, in requests 2.x,
    #  this raises requests.exceptions.MissingScheme
    requests.get("http://example.org", proxies=proxies)
    

Behavioural Changes¶

  • Keys in the headers dictionary are now native strings on all Python
    versions, i.e. bytestrings on Python 2 and unicode on Python 3. If the
    keys are not native strings (unicode on Python 2 or bytestrings on Python 3)
    they will be converted to the native string type assuming UTF-8 encoding.
  • Values in the headers dictionary should always be strings. This has
    been the project’s position since before 1.0 but a recent change
    (since version 2.11.0) enforces this more strictly. It’s advised to avoid
    passing header values as unicode when possible.

Понравилась статья? Поделить с друзьями:
  • Http error 422 unprocessable entity
  • Http error 500 beget
  • Http error 422 roblox studio
  • Http error 419
  • Http error 418 что это