Python Network Programming 简明教程

Python - HTTP Requests

http 或超文本传输协议基于客户端 - 服务器模型。通常,Web 浏览器是客户端,而承载网站的计算机是服务器。在 Python 中,我们使用请求模块创建 http 请求。这是一个非常强大的模块,可以处理超出简单请求和响应数据之外的许多 http 通信方面。它可以处理身份验证、压缩/解压缩、块请求等。

The http or Hyper Text Transfer Protocol works on client server model. Usually the web browser is the client and the computer hosting the website is the server. IN python we use the requests module for creating the http requests. It is a very powerful module which can handle many aspects of http communication beyond the simple request and response data. It can handle authentication, compression/decompression, chunked requests etc.

HTTP 客户端以请求消息的形式向服务器发送 HTTP 请求,其中包含以下格式:

An HTTP client sends an HTTP request to a server in the form of a request message which includes following format:

  1. A Request-line

  2. Zero or more header (General|Request|Entity) fields followed by CRLF

  3. An empty line (i.e., a line with nothing preceding the CRLF) indicating the end of the header fields

  4. Optionally a message-body

以下部分解释了 HTTP 请求消息中使用的每个实体。

The following sections explain each of the entities used in an HTTP request message.

Request-Line

请求行以方法标记开头,后跟请求 URI 和协议版本,并以 CRLF 结尾。各元素之间由空格 SP 字符分隔。

The Request-Line begins with a method token, followed by the Request-URI and the protocol version, and ending with CRLF. The elements are separated by space SP characters.

Request-Line = Method SP Request-URI SP HTTP-Version CRLF

下面讨论请求行中提到的各个部分。

Let’s discuss each of the parts mentioned in the Request-Line.

Request Method

请求 method 指示对由给定的 Request-URI 识别的资源执行的方法。该方法区分大小写,并且始终应大写提及。下表列出了 HTTP/1.1 中支持的所有方法。

The request method indicates the method to be performed on the resource identified by the given Request-URI. The method is case-sensitive and should always be mentioned in uppercase. The following table lists all the supported methods in HTTP/1.1.

S.N.

Method and Description

1

*GET*The GET method is used to retrieve information from the given server using a given URI. Requests using GET should only retrieve data and should have no other effect on the data.

2

*HEAD*Same as GET, but it transfers the status line and the header section only.

3

*POST*A POST request is used to send data to the server, for example, customer information, file upload, etc. using HTML forms.

4

*PUT*Replaces all the current representations of the target resource with the uploaded content.

5

*DELETE*Removes all the current representations of the target resource given by URI.

6

*CONNECT*Establishes a tunnel to the server identified by a given URI.

7

*OPTIONS*Describe the communication options for the target resource.

8

*TRACE*Performs a message loop back test along with the path to the target resource.

Request-URI

Request-URI 是统一资源标识符,用于标识对其应用请求的资源。下面是最常用的 URI 指定形式:

The Request-URI is a Uniform Resource Identifier and identifies the resource upon which to apply the request. Following are the most commonly used forms to specify an URI:

Request-URI = "*" | absoluteURI | abs_path | authority