http --- HTTP モジュール群

ソースコード: Lib/http/__init__.py


http パッケージはハイパーテキスト転送プロトコルを扱うための幾つかのモジュールを集めたものです:

  • http.client は低水準のHTTPプロトコルのクライアントです。高水準の URL を開く操作には urllib.request を使ってください

  • http.serversocketserver をベースにした基礎的な HTTP サーバーを実装しています

  • http.cookies は cookie の状態管理を実装するためのユーティリティーを提供しています

  • http.cookiejar は cookie の永続化機能を提供しています

The http module also defines the following enums that help you work with http related code:

class http.HTTPStatus

バージョン 3.5 で追加.

一連の HTTP ステータスコード、理由の表現、英語で書かれた長い記述を定義した enum.IntEnum のサブクラスです。

使い方:

>>> from http import HTTPStatus
>>> HTTPStatus.OK
HTTPStatus.OK
>>> HTTPStatus.OK == 200
True
>>> HTTPStatus.OK.value
200
>>> HTTPStatus.OK.phrase
'OK'
>>> HTTPStatus.OK.description
'Request fulfilled, document follows'
>>> list(HTTPStatus)
[HTTPStatus.CONTINUE, HTTPStatus.SWITCHING_PROTOCOLS, ...]

HTTP ステータスコード

http.HTTPStatus で利用可能な、サポートされている、IANA に登録されたステータスコード <https://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml>`_ は:

コード

列挙名

詳細

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

後方互換性を保つために列挙値は http.client にも定数という形で存在します。 列挙名は定数名と同じです (すなわち http.HTTPStatus.OKhttp.client.OK としても利用可能です)。

バージョン 3.7 で変更: ステータスコード 421 MISDIRECTED_REQUEST が追加されました。

バージョン 3.8 で追加: ステータスコード 451 UNAVAILABLE_FOR_LEGAL_REASONS が追加されました。

バージョン 3.9 で追加: ステータスコード 103 EARLY_HINTS, 418 IM_A_TEAPOT, 425 TOO_EARLY が追加されました。

HTTP status category

バージョン 3.12 で追加.

The enum values have several properties to indicate the HTTP status category:

Property

Indicates that

詳細

is_informational

100 <= status <= 199

HTTP/1.1 RFC 7231, Section 6

is_success

200 <= status <= 299

HTTP/1.1 RFC 7231, Section 6

is_redirection

300 <= status <= 399

HTTP/1.1 RFC 7231, Section 6

is_client_error

400 <= status <= 499

HTTP/1.1 RFC 7231, Section 6

is_server_error

500 <= status <= 599

HTTP/1.1 RFC 7231, Section 6

使い方:

>>> from http import HTTPStatus
>>> HTTPStatus.OK.is_success
True
>>> HTTPStatus.OK.is_client_error
False
class http.HTTPMethod

バージョン 3.11 で追加.

一連の HTTP メソッド、英語で書かれた記述を定義した enum.StrEnum のサブクラスです。

使い方:

>>> from http import HTTPMethod
>>>
>>> HTTPMethod.GET
<HTTPMethod.GET>
>>> HTTPMethod.GET == 'GET'
True
>>> HTTPMethod.GET.value
'GET'
>>> HTTPMethod.GET.description
'Retrieve the target.'
>>> list(HTTPMethod)
[<HTTPMethod.CONNECT>,
 <HTTPMethod.DELETE>,
 <HTTPMethod.GET>,
 <HTTPMethod.HEAD>,
 <HTTPMethod.OPTIONS>,
 <HTTPMethod.PATCH>,
 <HTTPMethod.POST>,
 <HTTPMethod.PUT>,
 <HTTPMethod.TRACE>]

HTTP methods

http.HTTPMethod で利用可能な、サポートされている、IANA に登録されたメソッド は:

メソッド

列挙名

詳細

GET

GET

HTTP/1.1 RFC 7231, Section 4.3.1

HEAD

HEAD

HTTP/1.1 RFC 7231, Section 4.3.2

POST

POST

HTTP/1.1 RFC 7231, Section 4.3.3

PUT

PUT

HTTP/1.1 RFC 7231, Section 4.3.4

DELETE

DELETE

HTTP/1.1 RFC 7231, Section 4.3.5

CONNECT

CONNECT

HTTP/1.1 RFC 7231, Section 4.3.6

OPTIONS

OPTIONS

HTTP/1.1 RFC 7231, Section 4.3.7

TRACE

TRACE

HTTP/1.1 RFC 7231, Section 4.3.8

PATCH

PATCH

HTTP/1.1 RFC 5789