HTTP 协议基础

HTTP 协议报文格式

图片来自 HTTP协议浅析(中):请求报文和响应报文

  • 请求报文
HTTP 协议基础_第1张图片
http_request.jpg
  • 响应报文
HTTP 协议基础_第2张图片
http_response.png

HTTP Satus Code

3xx Redirection

  • 304: NOT MODIFIED
    A conditional GET or HEAD request has been received and would have resulted in a 200 OK response if it were not for the fact that the condition evaluated to false.

4xx: Client Error

  • 401: UNAUTHORIZED
    The request has not been applied because it lacks valid authentication credentials for the target resource.

  • 404: NOT FOUND
    The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.

  • 405: METHOD NOT ALLOWED
    The method received in the request-line is known by the origin server but not supported by the target resource. (对于请求所标识的资源,不允许使用请求行中所指定的方法。)

  • 407: PROXY AUTHENTICATION REQUIRED
    Similar to 401 Unauthorized, but it indicates that the client needs to authenticate itself in order to use a proxy.

HTTP Methods

POST

  • 四种常见的 POST 提交数据方式:

    • application/x-www-form-urlencoded
    • multipart/form-data
    • application/json
    • text/xml
  • post Upload上传文件中multipart/form-data 做的那些事

HTTP headers

Etag

Etag(Entity tag) 具体解释: 什么是ETag

Tornado/1.1 web.py 源码:

            if (self._status_code == 200 and self.request.method == "GET" and
                "Etag" not in self._headers):
                hasher = hashlib.sha1()
                for part in self._write_buffer:
                    hasher.update(part)
                etag = '"%s"' % hasher.hexdigest()
                inm = self.request.headers.get("If-None-Match")
                if inm and inm.find(etag) != -1:
                    self._write_buffer = []
                    self.set_status(304)
                else:
                    self.set_header("Etag", etag)

location

跳转(重定向)
当浏览器接受到头信息中的 Location: xxxx 后,就会自动跳转到 xxxx 指向的URL地址.
例如,

header["location"] =  "http://www.baidu.com/"

keep-alive

具体阅读: HTTP Keep-Alive是什么?如何工作?

HTTP 协议基础_第3张图片
keep-alive

read more

  • HTTP Status Codes

  • Python documentation: http module

你可能感兴趣的:(HTTP 协议基础)