Python Network Programming 简明教程
Python - HTTP Response
http 或超文本传输协议在客户端服务器模型上运行。通常,Web 浏览器是客户端,而托管网站的计算机是服务器。在收到客户端的请求后,服务器生成响应并以特定格式将其发送回客户端。
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. Upon receiving a request from client the server generates a response and sends it back to the client in certain format.
在收到并解读请求消息后,服务器会使用 HTTP 响应消息进行响应:
After receiving and interpreting a request message, a server responds with an HTTP response message:
A Status-line
Zero or more header (General|Response|Entity) fields followed by CRLF
An empty line (i.e., a line with nothing preceding the CRLF)
indicating the end of the header fields
Optionally a message-body
下列各节将解释 HTTP 响应消息中使用的每个实体。
The following sections explain each of the entities used in an HTTP response message.
Message Status-Line
状态行由协议版本后跟一个数字状态代码及其关联的文本短语组成。元素由空格 SP 字符分隔。
A Status-Line consists of the protocol version followed by a numeric status code and its associated textual phrase. The elements are separated by space SP characters.
Status-Line = HTTP-Version SP Status-Code SP Reason-Phrase CRLF
HTTP Version
支持 HTTP 1.1 版本的服务器将返回以下版本信息:
A server supporting HTTP version 1.1 will return the following version information:
HTTP-Version = HTTP/1.1
Status Code
状态代码元素是 3 位整数,其中状态码的第一位定义了响应的类型,而最后两位没有分类作用。第一位有 5 个值:
The Status-Code element 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:
S.N. |
Code and Description |
1 |
*1xx: Informational*It means the request was received and the process is continuing. |
2 |
*2xx: Success*It means the action was successfully received, understood, and accepted. |
3 |
*3xx: Redirection*It means further action must be taken in order to complete the request. |
4 |
*4xx: Client Error*It means the request contains incorrect syntax or cannot be fulfilled. |
5 |
*5xx: Server Error*It means the server failed to fulfill an apparently valid request. |
HTTP 状态代码是可扩展的,且 HTTP 应用程序无需理解所有已注册状态代码的含义。
HTTP status codes are extensible and HTTP applications are not required to understand the meaning of all registered status codes.
Using Python Requests
在以下 Python 程序中,我们使用 urllib3 模块来发出 http GET 请求并接收包含数据的响应。它还提供响应代码,该代码也由模块中的函数管理。PoolManager 对象处理连接池的所有详细信息并处理线程安全。
In the below python program we use the urllib3 module to make a http GET request and receive the response containing the data. It also provides the response code which is also managed by the functions in the module. The PoolManager object handles all of the details of connection pooling and also handles the thread safety.
import urllib3
http = urllib3.PoolManager()
resp = http.request('GET', 'http://tutorialspoint.com/robots.txt')
print resp.data
# get the status of the response
print resp.status
当我们运行以上程序时,我们得到以下输出:
When we run the above program, we get the following output −
User-agent: *
Disallow: /tmp
Disallow: /logs
Disallow: /rate/*
Disallow: /cgi-bin/*
Disallow: /videotutorials/video_course_view.php?*
Disallow: /videotutorials/course_view.php?*
Disallow: /videos/*
Disallow: /*/*_question_bank/*
Disallow: //*/*/*/*/src/*
200