Документация Python неофициальный перевод

http.md

169 строк · 14.2 КБ · обычная страница · сырой текст · скачать

1> **Источник:** https://python-all.ru/3.11/library/http.html2>3> «Документация Python на русском» – неофициальный перевод официальной документации Python: версии от 2.6 до 3.16, полнотекстовый поиск, английский оригинал рядом с переводом. Эта Markdown-версия страницы предназначена для работы с LLM: вставьте её в ChatGPT, Claude или Cursor.45---67# [`http`](https://python-all.ru/3.11/library/http.html#module-http) – модули HTTP89**Исходный код:** [Lib/http/\_\_init\_\_.py](https://python-all.ru/src/3.11/Lib/http/__init__.py)1011---1213[`http`](https://python-all.ru/3.11/library/http.html#module-http) – пакет, объединяющий несколько модулей для работы с протоколом передачи гипертекста (HTTP):1415- [`http.client`](https://python-all.ru/3.11/library/http.client.html#module-http.client) – низкоуровневый клиент протокола HTTP; для открытия URL на высоком уровне используйте [`urllib.request`](https://python-all.ru/3.11/library/urllib.request.html#module-urllib.request)16- [`http.server`](https://python-all.ru/3.11/library/http.server.html#module-http.server) содержит базовые классы HTTP-сервера на основе [`socketserver`](https://python-all.ru/3.11/library/socketserver.html#module-socketserver)17- [`http.cookies`](https://python-all.ru/3.11/library/http.cookies.html#module-http.cookies) содержит утилиты для реализации управления состоянием с помощью куки18- [`http.cookiejar`](https://python-all.ru/3.11/library/http.cookiejar.html#module-http.cookiejar) обеспечивает сохранение куки1920Модуль [`http`](https://python-all.ru/3.11/library/http.html#module-http) также определяет следующие перечисления, которые помогают работать с кодом, связанным с HTTP:2122#### `class http.HTTPStatus`2324Новое в версии 3.5.2526Подкласс [`enum.IntEnum`](https://python-all.ru/3.11/library/enum.html#enum.IntEnum), определяющий набор HTTP-статусов, поясняющих фраз и подробных описаний на английском языке.2728Использование:2930```python31>>> from http import HTTPStatus32>>> HTTPStatus.OK33HTTPStatus.OK34>>> HTTPStatus.OK == 20035True36>>> HTTPStatus.OK.value3720038>>> HTTPStatus.OK.phrase39'OK'40>>> HTTPStatus.OK.description41'Request fulfilled, document follows'42>>> list(HTTPStatus)43[HTTPStatus.CONTINUE, HTTPStatus.SWITCHING_PROTOCOLS, ...]44```4546## Коды состояния HTTP4748Поддерживаемые [зарегистрированные IANA коды состояния](https://python-all.ru/3.11/library/http.html), доступные в [`http.HTTPStatus`](https://python-all.ru/3.11/library/http.html#http.HTTPStatus):4950| Код | Имя перечисления | Подробности |51| --- | --- | --- |52| `100` | `CONTINUE` | HTTP/1.1 [**RFC 7231**](https://python-all.ru/3.11/library/http.html), раздел 6.2.1 |53| `101` | `SWITCHING_PROTOCOLS` | HTTP/1.1 [**RFC 7231**](https://python-all.ru/3.11/library/http.html), раздел 6.2.2 |54| `102` | `PROCESSING` | WebDAV [**RFC 2518**](https://python-all.ru/3.11/library/http.html), раздел 10.1 |55| `103` | `EARLY_HINTS` | Код состояния HTTP для указания подсказок [**RFC 8297**](https://python-all.ru/3.11/library/http.html) |56| `200` | `OK` | HTTP/1.1 [**RFC 7231**](https://python-all.ru/3.11/library/http.html), раздел 6.3.1 |57| `201` | `CREATED` | HTTP/1.1 [**RFC 7231**](https://python-all.ru/3.11/library/http.html), раздел 6.3.2 |58| `202` | `ACCEPTED` | HTTP/1.1 [**RFC 7231**](https://python-all.ru/3.11/library/http.html), раздел 6.3.3 |59| `203` | `NON_AUTHORITATIVE_INFORMATION` | HTTP/1.1 [**RFC 7231**](https://python-all.ru/3.11/library/http.html), раздел 6.3.4 |60| `204` | `NO_CONTENT` | HTTP/1.1 [**RFC 7231**](https://python-all.ru/3.11/library/http.html), раздел 6.3.5 |61| `205` | `RESET_CONTENT` | HTTP/1.1 [**RFC 7231**](https://python-all.ru/3.11/library/http.html), раздел 6.3.6 |62| `206` | `PARTIAL_CONTENT` | HTTP/1.1 [**RFC 7233**](https://python-all.ru/3.11/library/http.html), раздел 4.1 |63| `207` | `MULTI_STATUS` | WebDAV [**RFC 4918**](https://python-all.ru/3.11/library/http.html), раздел 11.1 |64| `208` | `ALREADY_REPORTED` | Расширения привязки WebDAV [**RFC 5842**](https://python-all.ru/3.11/library/http.html), раздел 7.1 (экспериментальный) |65| `226` | `IM_USED` | Дельта-кодирование в HTTP [**RFC 3229**](https://python-all.ru/3.11/library/http.html), раздел 10.4.1 |66| `300` | `MULTIPLE_CHOICES` | HTTP/1.1 [**RFC 7231**](https://python-all.ru/3.11/library/http.html), раздел 6.4.1 |67| `301` | `MOVED_PERMANENTLY` | HTTP/1.1 [**RFC 7231**](https://python-all.ru/3.11/library/http.html), раздел 6.4.2 |68| `302` | `FOUND` | HTTP/1.1 [**RFC 7231**](https://python-all.ru/3.11/library/http.html), раздел 6.4.3 |69| `303` | `SEE_OTHER` | HTTP/1.1 [**RFC 7231**](https://python-all.ru/3.11/library/http.html), раздел 6.4.4 |70| `304` | `NOT_MODIFIED` | HTTP/1.1 [**RFC 7232**](https://python-all.ru/3.11/library/http.html), раздел 4.1 |71| `305` | `USE_PROXY` | HTTP/1.1 [**RFC 7231**](https://python-all.ru/3.11/library/http.html), раздел 6.4.5 |72| `307` | `TEMPORARY_REDIRECT` | HTTP/1.1 [**RFC 7231**](https://python-all.ru/3.11/library/http.html), раздел 6.4.7 |73| `308` | `PERMANENT_REDIRECT` | Постоянное перенаправление [**RFC 7238**](https://python-all.ru/3.11/library/http.html), раздел 3 (экспериментальный) |74| `400` | `BAD_REQUEST` | HTTP/1.1 [**RFC 7231**](https://python-all.ru/3.11/library/http.html), раздел 6.5.1 |75| `401` | `UNAUTHORIZED` | HTTP/1.1 аутентификация [**RFC 7235**](https://python-all.ru/3.11/library/http.html), раздел 3.1 |76| `402` | `PAYMENT_REQUIRED` | HTTP/1.1 [**RFC 7231**](https://python-all.ru/3.11/library/http.html), раздел 6.5.2 |77| `403` | `FORBIDDEN` | HTTP/1.1 [**RFC 7231**](https://python-all.ru/3.11/library/http.html), раздел 6.5.3 |78| `404` | `NOT_FOUND` | HTTP/1.1 [**RFC 7231**](https://python-all.ru/3.11/library/http.html), раздел 6.5.4 |79| `405` | `METHOD_NOT_ALLOWED` | HTTP/1.1 [**RFC 7231**](https://python-all.ru/3.11/library/http.html), раздел 6.5.5 |80| `406` | `NOT_ACCEPTABLE` | HTTP/1.1 [**RFC 7231**](https://python-all.ru/3.11/library/http.html), раздел 6.5.6 |81| `407` | `PROXY_AUTHENTICATION_REQUIRED` | HTTP/1.1 аутентификация [**RFC 7235**](https://python-all.ru/3.11/library/http.html), раздел 3.2 |82| `408` | `REQUEST_TIMEOUT` | HTTP/1.1 [**RFC 7231**](https://python-all.ru/3.11/library/http.html), раздел 6.5.7 |83| `409` | `CONFLICT` | HTTP/1.1 [**RFC 7231**](https://python-all.ru/3.11/library/http.html), раздел 6.5.8 |84| `410` | `GONE` | HTTP/1.1 [**RFC 7231**](https://python-all.ru/3.11/library/http.html), раздел 6.5.9 |85| `411` | `LENGTH_REQUIRED` | HTTP/1.1 [**RFC 7231**](https://python-all.ru/3.11/library/http.html), раздел 6.5.10 |86| `412` | `PRECONDITION_FAILED` | HTTP/1.1 [**RFC 7232**](https://python-all.ru/3.11/library/http.html), раздел 4.2 |87| `413` | `REQUEST_ENTITY_TOO_LARGE` | HTTP/1.1 [**RFC 7231**](https://python-all.ru/3.11/library/http.html), раздел 6.5.11 |88| `414` | `REQUEST_URI_TOO_LONG` | HTTP/1.1 [**RFC 7231**](https://python-all.ru/3.11/library/http.html), раздел 6.5.12 |89| `415` | `UNSUPPORTED_MEDIA_TYPE` | HTTP/1.1 [**RFC 7231**](https://python-all.ru/3.11/library/http.html), раздел 6.5.13 |90| `416` | `REQUESTED_RANGE_NOT_SATISFIABLE` | HTTP/1.1 запросы диапазона [**RFC 7233**](https://python-all.ru/3.11/library/http.html), раздел 4.4 |91| `417` | `EXPECTATION_FAILED` | HTTP/1.1 [**RFC 7231**](https://python-all.ru/3.11/library/http.html), раздел 6.5.14 |92| `418` | `IM_A_TEAPOT` | HTCPCP/1.0 [**RFC 2324**](https://python-all.ru/3.11/library/http.html), Раздел 2.3.2 |93| `421` | `MISDIRECTED_REQUEST` | HTTP/2 [**RFC 7540**](https://python-all.ru/3.11/library/http.html), раздел 9.1.2 |94| `422` | `UNPROCESSABLE_ENTITY` | WebDAV [**RFC 4918**](https://python-all.ru/3.11/library/http.html), раздел 11.2 |95| `423` | `LOCKED` | WebDAV [**RFC 4918**](https://python-all.ru/3.11/library/http.html), Раздел 11.3 |96| `424` | `FAILED_DEPENDENCY` | WebDAV [**RFC 4918**](https://python-all.ru/3.11/library/http.html), Раздел 11.4 |97| `425` | `TOO_EARLY` | Использование ранних данных в HTTP [**RFC 8470**](https://python-all.ru/3.11/library/http.html) |98| `426` | `UPGRADE_REQUIRED` | HTTP/1.1 [**RFC 7231**](https://python-all.ru/3.11/library/http.html), раздел 6.5.15 |99| `428` | `PRECONDITION_REQUIRED` | Дополнительные коды состояния HTTP [**RFC 6585**](https://python-all.ru/3.11/library/http.html) |100| `429` | `TOO_MANY_REQUESTS` | Дополнительные коды состояния HTTP [**RFC 6585**](https://python-all.ru/3.11/library/http.html) |101| `431` | `REQUEST_HEADER_FIELDS_TOO_LARGE` | Дополнительные коды состояния HTTP [**RFC 6585**](https://python-all.ru/3.11/library/http.html) |102| `451` | `UNAVAILABLE_FOR_LEGAL_REASONS` | Код состояния HTTP для сообщения о юридических препятствиях [**RFC 7725**](https://python-all.ru/3.11/library/http.html) |103| `500` | `INTERNAL_SERVER_ERROR` | HTTP/1.1 [**RFC 7231**](https://python-all.ru/3.11/library/http.html), раздел 6.6.1 |104| `501` | `NOT_IMPLEMENTED` | HTTP/1.1 [**RFC 7231**](https://python-all.ru/3.11/library/http.html), раздел 6.6.2 |105| `502` | `BAD_GATEWAY` | HTTP/1.1 [**RFC 7231**](https://python-all.ru/3.11/library/http.html), раздел 6.6.3 |106| `503` | `SERVICE_UNAVAILABLE` | HTTP/1.1 [**RFC 7231**](https://python-all.ru/3.11/library/http.html), раздел 6.6.4 |107| `504` | `GATEWAY_TIMEOUT` | HTTP/1.1 [**RFC 7231**](https://python-all.ru/3.11/library/http.html), раздел 6.6.5 |108| `505` | `HTTP_VERSION_NOT_SUPPORTED` | HTTP/1.1 [**RFC 7231**](https://python-all.ru/3.11/library/http.html), раздел 6.6.6 |109| `506` | `VARIANT_ALSO_NEGOTIATES` | Прозрачное согласование содержимого в HTTP [**RFC 2295**](https://python-all.ru/3.11/library/http.html), раздел 8.1 (экспериментальный) |110| `507` | `INSUFFICIENT_STORAGE` | WebDAV [**RFC 4918**](https://python-all.ru/3.11/library/http.html), раздел 11.5 |111| `508` | `LOOP_DETECTED` | Расширения привязки WebDAV [**RFC 5842**](https://python-all.ru/3.11/library/http.html), раздел 7.2 (экспериментальный) |112| `510` | `NOT_EXTENDED` | Расширяемая структура HTTP [**RFC 2774**](https://python-all.ru/3.11/library/http.html), раздел 7 (экспериментальный) |113| `511` | `NETWORK_AUTHENTICATION_REQUIRED` | Дополнительные коды состояния HTTP [**RFC 6585**](https://python-all.ru/3.11/library/http.html), раздел 6 |114115Для сохранения обратной совместимости значения перечисления также присутствуют в модуле [`http.client`](https://python-all.ru/3.11/library/http.client.html#module-http.client) в виде констант. Имя перечисления совпадает с именем константы (т.е. `http.HTTPStatus.OK` также доступно как `http.client.OK`).116117Изменено в версии 3.7: Добавлен код состояния `421 MISDIRECTED_REQUEST`.118119Новое в версии 3.8: Добавлен код состояния `451 UNAVAILABLE_FOR_LEGAL_REASONS`.120121Новое в версии 3.9: Добавлены коды состояния `103 EARLY_HINTS`, `418 IM_A_TEAPOT` и `425 TOO_EARLY`.122123#### `class http.HTTPMethod`124125Новое в версии 3.11.126127Подкласс [`enum.StrEnum`](https://python-all.ru/3.11/library/enum.html#enum.StrEnum), определяющий набор HTTP-методов и описаний на английском языке.128129Использование:130131```python132>>> from http import HTTPMethod133>>>134>>> HTTPMethod.GET135<HTTPMethod.GET>136>>> HTTPMethod.GET == 'GET'137True138>>> HTTPMethod.GET.value139'GET'140>>> HTTPMethod.GET.description141'Retrieve the target.'142>>> list(HTTPMethod)143[<HTTPMethod.CONNECT>,144 <HTTPMethod.DELETE>,145 <HTTPMethod.GET>,146 <HTTPMethod.HEAD>,147 <HTTPMethod.OPTIONS>,148 <HTTPMethod.PATCH>,149 <HTTPMethod.POST>,150 <HTTPMethod.PUT>,151 <HTTPMethod.TRACE>]152```153154## Методы HTTP155156Поддерживаемые, [зарегистрированные в IANA методы](https://python-all.ru/3.11/library/http.html) доступные в [`http.HTTPMethod`](https://python-all.ru/3.11/library/http.html#http.HTTPMethod):157158| Метод | Имя перечисления | Подробности |159| --- | --- | --- |160| `GET` | `GET` | HTTP/1.1 [**RFC 7231**](https://python-all.ru/3.11/library/http.html), раздел 4.3.1 |161| `HEAD` | `HEAD` | HTTP/1.1 [**RFC 7231**](https://python-all.ru/3.11/library/http.html), раздел 4.3.2 |162| `POST` | `POST` | HTTP/1.1 [**RFC 7231**](https://python-all.ru/3.11/library/http.html), раздел 4.3.3 |163| `PUT` | `PUT` | HTTP/1.1 [**RFC 7231**](https://python-all.ru/3.11/library/http.html), раздел 4.3.4 |164| `DELETE` | `DELETE` | HTTP/1.1 [**RFC 7231**](https://python-all.ru/3.11/library/http.html), раздел 4.3.5 |165| `CONNECT` | `CONNECT` | HTTP/1.1 [**RFC 7231**](https://python-all.ru/3.11/library/http.html), раздел 4.3.6 |166| `OPTIONS` | `OPTIONS` | HTTP/1.1 [**RFC 7231**](https://python-all.ru/3.11/library/http.html), раздел 4.3.7 |167| `TRACE` | `TRACE` | HTTP/1.1 [**RFC 7231**](https://python-all.ru/3.11/library/http.html), раздел 4.3.8 |168| `PATCH` | `PATCH` | HTTP/1.1 [**RFC 5789**](https://python-all.ru/3.11/library/http.html) |169