Python — Request Status Codes
After receiving and interpreting a request message, a server responds with an HTTP response message. The response message has a Status-Code. It is a 3-digit integer where first digit of the Status-Code defines the class of response and the last two digits do not have any categorization role. There are 5 values for the first digit:
Status Codes
| S.N. | Code and Description |
|---|---|
| 1 | 1xx: Informational |
It means the request was received and the process is continuing.
It means the action was successfully received, understood, and accepted.
It means further action must be taken in order to complete the request.
It means the request contains incorrect syntax or cannot be fulfilled.
It means the server failed to fulfill an apparently valid request.
Successful Response
In the below example we access a file from a url and the response is successful. So the status code returned is 200.
When we run the above program, we get the following output −
Unsuccessful Response
In the below example we access a file from a url which does not exist. The response is unsuccessful. So the status code returned is 403.
http — HTTP модули¶
http — пакет, который объединяет несколько модулей для работы с протоколом передачи гипертекста:
-
— клиент протокола HTTP низкого уровня; для открытия URL- адреса высокого уровня используйте urllib.request содержит базовые классы HTTP-серверов на основе socketserver имеет утилиты для реализации управления состоянием с помощью cookie обеспечивает сохранение cookie
http также является модулем, который определяет ряд кодов состояния HTTP и связанных сообщений через перечисление http.HTTPStatus :
class http. HTTPStatus ¶
Добавлено в версии 3.5.
Подкласс enum.IntEnum , определяет множество кодов состояния HTTP, фраз причины и длинных описаний, написанных на английском языке.
Коды состояния HTTP¶
Поддерживаются коды состояния IANA-registered, доступные в http.HTTPStatus :
| Код | Перечисление имён | Подробности |
|---|---|---|
| 100 | CONTINUE | HTTP/1.1 RFC 7231, Раздел 6.2.1 |
| 101 | SWITCHING_PROTOCOLS | HTTP/1.1 RFC 7231, Раздел 6.2.2 |
| 102 | PROCESSING | WebDAV RFC 2518, Раздел 10.1 |
| 200 | OK | HTTP/1.1 RFC 7231, Раздел 6.3.1 |
| 201 | CREATED | HTTP/1.1 RFC 7231, Раздел 6.3.2 |
| 202 | ACCEPTED | HTTP/1.1 RFC 7231, Раздел 6.3.3 |
| 203 | NON_AUTHORITATIVE_INFORMATION | HTTP/1.1 RFC 7231, Раздел 6.3.4 |
| 204 | NO_CONTENT | HTTP/1.1 RFC 7231, Раздел 6.3.5 |
| 205 | RESET_CONTENT | HTTP/1.1 RFC 7231, Раздел 6.3.6 |
| 206 | PARTIAL_CONTENT | HTTP/1.1 RFC 7233, Раздел 4.1 |
| 207 | MULTI_STATUS | WebDAV RFC 4918, Раздел 11.1 |
| 208 | ALREADY_REPORTED | Расширения привязки WebDAV RFC 5842, Раздел 7.1 (Экспериментальный) |
| 226 | IM_USED | Дельта-кодирование в HTTP RFC 3229, Раздел 10.4.1 |
| 300 | MULTIPLE_CHOICES | HTTP/1.1 RFC 7231, Раздел 6.4.1 |
| 301 | MOVED_PERMANENTLY | HTTP/1.1 RFC 7231, Раздел 6.4.2 |
| 302 | FOUND | HTTP/1.1 RFC 7231, Раздел 6.4.3 |
| 303 | SEE_OTHER | HTTP/1.1 RFC 7231, Раздел 6.4.4 |
| 304 | NOT_MODIFIED | HTTP/1.1 RFC 7232, Раздел 4.1 |
| 305 | USE_PROXY | HTTP/1.1 RFC 7231, Раздел 6.4.5 |
| 307 | TEMPORARY_REDIRECT | HTTP/1.1 RFC 7231, Раздел 6.4.7 |
| 308 | PERMANENT_REDIRECT | Постоянное перенаправление RFC 7238, Раздел 3 (Экспериментальный) |
| 400 | BAD_REQUEST | HTTP/1.1 RFC 7231, Раздел 6.5.1 |
| 401 | UNAUTHORIZED | HTTP/1.1 Идентификация RFC 7235, Раздел 3.1 |
| 402 | PAYMENT_REQUIRED | HTTP/1.1 RFC 7231, Раздел 6.5.2 |
| 403 | FORBIDDEN | HTTP/1.1 RFC 7231, Раздел 6.5.3 |
| 404 | NOT_FOUND | HTTP/1.1 RFC 7231, Раздел 6.5.4 |
| 405 | METHOD_NOT_ALLOWED | HTTP/1.1 RFC 7231, Раздел 6.5.5 |
| 406 | NOT_ACCEPTABLE | HTTP/1.1 RFC 7231, Раздел 6.5.6 |
| 407 | PROXY_AUTHENTICATION_REQUIRED | HTTP/1.1 Идентификация RFC 7235, Раздел 3.2 |
| 408 | REQUEST_TIMEOUT | HTTP/1.1 RFC 7231, Раздел 6.5.7 |
| 409 | CONFLICT | HTTP/1.1 RFC 7231, Раздел 6.5.8 |
| 410 | GONE | HTTP/1.1 RFC 7231, Раздел 6.5.9 |
| 411 | LENGTH_REQUIRED | HTTP/1.1 RFC 7231, Раздел 6.5.10 |
| 412 | PRECONDITION_FAILED | HTTP/1.1 RFC 7232, Раздел 4.2 |
| 413 | REQUEST_ENTITY_TOO_LARGE | HTTP/1.1 RFC 7231, Раздел 6.5.11 |
| 414 | REQUEST_URI_TOO_LONG | HTTP/1.1 RFC 7231, Раздел 6.5.12 |
| 415 | UNSUPPORTED_MEDIA_TYPE | HTTP/1.1 RFC 7231, Раздел 6.5.13 |
| 416 | REQUESTED_RANGE_NOT_SATISFIABLE | HTTP/1.1 Диапазон запросов RFC 7233, Раздел 4.4 |
| 417 | EXPECTATION_FAILED | HTTP/1.1 RFC 7231, Раздел 6.5.14 |
| 421 | MISDIRECTED_REQUEST | HTTP/2 RFC 7540, Раздел 9.1.2 |
| 422 | UNPROCESSABLE_ENTITY | WebDAV RFC 4918, Раздел 11.2 |
| 423 | LOCKED | WebDAV RFC 4918, Раздел 11.3 |
| 424 | FAILED_DEPENDENCY | WebDAV RFC 4918, Раздел 11.4 |
| 426 | UPGRADE_REQUIRED | HTTP/1.1 RFC 7231, Раздел 6.5.15 |
| 428 | PRECONDITION_REQUIRED | Дополнительные Коды состояния HTTP RFC 6585 |
| 429 | TOO_MANY_REQUESTS | Дополнительные Коды состояния HTTP RFC 6585 |
| 431 | REQUEST_HEADER_FIELDS_TOO_LARGE | Дополнительные Коды состояния HTTP RFC 6585 |
| 451 | UNAVAILABLE_FOR_LEGAL_REASONS | Код состояния HTTP для сообщения о юридических препятствиях RFC 7725 |
| 500 | INTERNAL_SERVER_ERROR | HTTP/1.1 RFC 7231, Раздел 6.6.1 |
| 501 | NOT_IMPLEMENTED | HTTP/1.1 RFC 7231, Раздел 6.6.2 |
| 502 | BAD_GATEWAY | HTTP/1.1 RFC 7231, Раздел 6.6.3 |
| 503 | SERVICE_UNAVAILABLE | HTTP/1.1 RFC 7231, Раздел 6.6.4 |
| 504 | GATEWAY_TIMEOUT | HTTP/1.1 RFC 7231, Раздел 6.6.5 |
| 505 | HTTP_VERSION_NOT_SUPPORTED | HTTP/1.1 RFC 7231, Раздел 6.6.6 |
| 506 | VARIANT_ALSO_NEGOTIATES | Прозрачное согласование содержимого в HTTP RFC 2295, Раздел 8.1 (Экспериментальный) |
| 507 | INSUFFICIENT_STORAGE | WebDAV RFC 4918, Раздел 11.5 |
| 508 | LOOP_DETECTED | Расширения привязки WebDAV RFC 5842, Раздел 7.2 (Экспериментальный) |
| 510 | NOT_EXTENDED | Фреймворк расширения HTTP RFC 2774, Раздел 7 (Экспериментальный) |
| 511 | NETWORK_AUTHENTICATION_REQUIRED | Дополнительные Коды состояния HTTP RFC 6585, Раздел 6 |
Чтобы сохранить обратную совместимость, значения перечисления также присутствуют в модуле http.client в виде констант. Имя перечисления равно имени константы (т. е. http.HTTPStatus.OK также доступно как http.client.OK ).
Изменено в версии 3.7: Добавлен код состояния 421 MISDIRECTED_REQUEST .
Добавлено в версии 3.8: Добавлен код состояния 451 UNAVAILABLE_FOR_LEGAL_REASONS .
Name already in use
cpython / Doc / library / http.rst
- Go to file T
- Go to line L
- Copy path
- Copy permalink
- Open with Desktop
- View raw
- Copy raw contents Copy raw contents
Copy raw contents
Copy raw contents
:mod:`http` is a package that collects several modules for working with the HyperText Transfer Protocol:
-
is a low-level HTTP protocol client; for high-level URL opening use :mod:`urllib.request` contains basic HTTP server classes based on :mod:`socketserver` has utilities for implementing state management with cookies provides persistence of cookies
The :mod:`http` module also defines the following enums that help you work with http related code:
A subclass of :class:`enum.IntEnum` that defines a set of HTTP status codes, reason phrases and long descriptions written in English.
HTTP status codes
| Code | Enum Name | Details |
|---|---|---|
| 100 | CONTINUE | HTTP/1.1 RFC 7231, Section 6.2.1 |
| 101 | SWITCHING_PROTOCOLS | HTTP/1.1 RFC 7231, Section 6.2.2 |
| 102 | PROCESSING | WebDAV RFC 2518, Section 10.1 |
| 103 | EARLY_HINTS | An HTTP Status Code for Indicating Hints RFC 8297 |
| 200 | OK | HTTP/1.1 RFC 7231, Section 6.3.1 |
| 201 | CREATED | HTTP/1.1 RFC 7231, Section 6.3.2 |
| 202 | ACCEPTED | HTTP/1.1 RFC 7231, Section 6.3.3 |
| 203 | NON_AUTHORITATIVE_INFORMATION | HTTP/1.1 RFC 7231, Section 6.3.4 |
| 204 | NO_CONTENT | HTTP/1.1 RFC 7231, Section 6.3.5 |
| 205 | RESET_CONTENT | HTTP/1.1 RFC 7231, Section 6.3.6 |
| 206 | PARTIAL_CONTENT | HTTP/1.1 RFC 7233, Section 4.1 |
| 207 | MULTI_STATUS | WebDAV RFC 4918, Section 11.1 |
| 208 | ALREADY_REPORTED | WebDAV Binding Extensions RFC 5842, Section 7.1 (Experimental) |
| 226 | IM_USED | Delta Encoding in HTTP RFC 3229, Section 10.4.1 |
| 300 | MULTIPLE_CHOICES | HTTP/1.1 RFC 7231, Section 6.4.1 |
| 301 | MOVED_PERMANENTLY | HTTP/1.1 RFC 7231, Section 6.4.2 |
| 302 | FOUND | HTTP/1.1 RFC 7231, Section 6.4.3 |
| 303 | SEE_OTHER | HTTP/1.1 RFC 7231, Section 6.4.4 |
| 304 | NOT_MODIFIED | HTTP/1.1 RFC 7232, Section 4.1 |
| 305 | USE_PROXY | HTTP/1.1 RFC 7231, Section 6.4.5 |
| 307 | TEMPORARY_REDIRECT | HTTP/1.1 RFC 7231, Section 6.4.7 |
| 308 | PERMANENT_REDIRECT | Permanent Redirect RFC 7238, Section 3 (Experimental) |
| 400 | BAD_REQUEST | HTTP/1.1 RFC 7231, Section 6.5.1 |
| 401 | UNAUTHORIZED | HTTP/1.1 Authentication RFC 7235, Section 3.1 |
| 402 | PAYMENT_REQUIRED | HTTP/1.1 RFC 7231, Section 6.5.2 |
| 403 | FORBIDDEN | HTTP/1.1 RFC 7231, Section 6.5.3 |
| 404 | NOT_FOUND | HTTP/1.1 RFC 7231, Section 6.5.4 |
| 405 | METHOD_NOT_ALLOWED | HTTP/1.1 RFC 7231, Section 6.5.5 |
| 406 | NOT_ACCEPTABLE | HTTP/1.1 RFC 7231, Section 6.5.6 |
| 407 | PROXY_AUTHENTICATION_REQUIRED | HTTP/1.1 Authentication RFC 7235, Section 3.2 |
| 408 | REQUEST_TIMEOUT | HTTP/1.1 RFC 7231, Section 6.5.7 |
| 409 | CONFLICT | HTTP/1.1 RFC 7231, Section 6.5.8 |
| 410 | GONE | HTTP/1.1 RFC 7231, Section 6.5.9 |
| 411 | LENGTH_REQUIRED | HTTP/1.1 RFC 7231, Section 6.5.10 |
| 412 | PRECONDITION_FAILED | HTTP/1.1 RFC 7232, Section 4.2 |
| 413 | REQUEST_ENTITY_TOO_LARGE | HTTP/1.1 RFC 7231, Section 6.5.11 |
| 414 | REQUEST_URI_TOO_LONG | HTTP/1.1 RFC 7231, Section 6.5.12 |
| 415 | UNSUPPORTED_MEDIA_TYPE | HTTP/1.1 RFC 7231, Section 6.5.13 |
| 416 | REQUESTED_RANGE_NOT_SATISFIABLE | HTTP/1.1 Range Requests RFC 7233, Section 4.4 |
| 417 | EXPECTATION_FAILED | HTTP/1.1 RFC 7231, Section 6.5.14 |
| 418 | IM_A_TEAPOT | HTCPCP/1.0 RFC 2324, Section 2.3.2 |
| 421 | MISDIRECTED_REQUEST | HTTP/2 RFC 7540, Section 9.1.2 |
| 422 | UNPROCESSABLE_ENTITY | WebDAV RFC 4918, Section 11.2 |
| 423 | LOCKED | WebDAV RFC 4918, Section 11.3 |
| 424 | FAILED_DEPENDENCY | WebDAV RFC 4918, Section 11.4 |
| 425 | TOO_EARLY | Using Early Data in HTTP RFC 8470 |
| 426 | UPGRADE_REQUIRED | HTTP/1.1 RFC 7231, Section 6.5.15 |
| 428 | PRECONDITION_REQUIRED | Additional HTTP Status Codes RFC 6585 |
| 429 | TOO_MANY_REQUESTS | Additional HTTP Status Codes RFC 6585 |
| 431 | REQUEST_HEADER_FIELDS_TOO_LARGE | Additional HTTP Status Codes RFC 6585 |
| 451 | UNAVAILABLE_FOR_LEGAL_REASONS | An HTTP Status Code to Report Legal Obstacles RFC 7725 |
| 500 | INTERNAL_SERVER_ERROR | HTTP/1.1 RFC 7231, Section 6.6.1 |
| 501 | NOT_IMPLEMENTED | HTTP/1.1 RFC 7231, Section 6.6.2 |
| 502 | BAD_GATEWAY | HTTP/1.1 RFC 7231, Section 6.6.3 |
| 503 | SERVICE_UNAVAILABLE | HTTP/1.1 RFC 7231, Section 6.6.4 |
| 504 | GATEWAY_TIMEOUT | HTTP/1.1 RFC 7231, Section 6.6.5 |
| 505 | HTTP_VERSION_NOT_SUPPORTED | HTTP/1.1 RFC 7231, Section 6.6.6 |
| 506 | VARIANT_ALSO_NEGOTIATES | Transparent Content Negotiation in HTTP RFC 2295, Section 8.1 (Experimental) |
| 507 | INSUFFICIENT_STORAGE | WebDAV RFC 4918, Section 11.5 |
| 508 | LOOP_DETECTED | WebDAV Binding Extensions RFC 5842, Section 7.2 (Experimental) |
| 510 | NOT_EXTENDED | An HTTP Extension Framework RFC 2774, Section 7 (Experimental) |
| 511 | NETWORK_AUTHENTICATION_REQUIRED | Additional HTTP Status Codes RFC 6585, Section 6 |
In order to preserve backwards compatibility, enum values are also present in the :mod:`http.client` module in the form of constants. The enum name is equal to the constant name (i.e. http.HTTPStatus.OK is also available as http.client.OK ).
HTTP status category
The enum values have several properties to indicate the HTTP status category:
Responses: Status Codes

What are HTTP status codes? Which status codes do exist? These questions are answered in this lesson of the Making HTTP Requests With Python course. You’ll learn about the different status code ranges and what they mean internally. Additionally, you’ll learn how to handle different status codes in multiple ways, e.g. by using if -statements or try-except -blocks.
balakumaranrk on Feb. 8, 2020
In this video what is the purpose of writing :
can you please explain ?
Ricky White RP Team on Feb. 8, 2020
This is used so that no exception is raised on a successful request. This will allow you to capture unsuccessful requests based on the exception type returned and deal with them accordingly.
idosegev on Feb. 12, 2020
I don’t get any response from my if-elif loop and i don’t know why. maybe it has to do with python38 ?
if response.status_code == 202: print(‘Success!’) elif response.status_code == 404: print(‘Not Found.’)

Chris Bailey RP Team on Feb. 12, 2020
There are a couple of differences from your code in the comment you posted and the example in the video lesson:
The url should be url = ‘http://api.github.com’ The first if should be if response.status_code == 200: and not 202 . 200 would mean success and 202 would mean something different, accepted. Because the code I’m demonstrating is an incomplete example, I don’t have a fall back of else: which would catch some other result. I guess you could have it print the response, such as else: print(f’Status Code was
Using an f string. I hope this helps with your question.
idosegev on Feb. 13, 2020
@Chris Bailey thanks for the quick response!
B S K Karthik on April 19, 2020
@Chris Bailey, Hi Chris Can you please let me know what color theme have you used in visual studio code editor. Thank you.

Chris Bailey RP Team on April 19, 2020
Hi @B S K Karthik, I use Dainty — Material Theme Ocean, in VSCode. Its part of the Dainty theme package. I love the contrast.