Home  >  Article  >  Backend Development  >  What does header mean in python?

What does header mean in python?

爱喝马黛茶的安东尼
爱喝马黛茶的安东尼Original
2019-06-26 18:04:2818787browse

Usually HTTP messages include request messages from the client to the server and response messages from the server to the client. Both types of messages consist of a start line, one or more header fields, a blank line just to end the header fields, and an optional message body. The HTTP header field includes four parts: general header, request header, response header and entity header. Each header field consists of a domain name, a colon (:) and a domain value. Domain names are case-insensitive. Any number of spaces can be added before the field value. Header fields can be expanded to multiple lines, using at least one space or tab at the beginning of each line.

What does header mean in python?

General header field (general header)

The general header field contains header fields supported by both request and response messages, providing The most basic information related to the message, the common header field contains:

Connection: allows the client and server to specify options related to the request/response connection.

Date: Provide date and time stamp to indicate when the message was created.

MIME-Version: Gives the MIME version used by the sender.

Trailer: If the message uses chunked transfer encoding, you can use this header to list the set of headers located in the trailer part of the message.

Transfer-Encoding: Inform the receiving end what encoding method is used for the message in order to ensure reliable transmission of the message.

Upgrade: Gives the new version and protocol that the sender may want to "upgrade" to use.

Via: Shows the intermediate nodes through which the message passes.

The extension of the general header field requires that both communicating parties support this extension. If there is an unsupported general header field, it will generally be processed as an entity header field. The following is a brief introduction to several common header fields used in UPnP messages.

Cache-Control header field

Cache-Control specifies the caching mechanism that requests and responses follow. Setting Cache-Control in a request message or response message does not modify the caching process during the processing of another message.

The cache instructions when requesting include no-cache, no-store, max-age, max-stale, min-fresh, and only-if-cached.

The instructions in the response message include public, private, no-cache, no-store, no-transform, must-revalidate, proxy-revalidate, and max-age.

The meaning of the instructions in each message is as follows:

Public indicates that the response can be cached by any cache area.

Private indicates that all or part of the response message for a single user cannot be processed by the shared cache. This allows the server to only describe a partial response from a user that is not valid for other users' requests.

no-cache indicates that the request or response message cannot be cached.

no-store is used to prevent important information from being released unintentionally. Sending it in the request message will cause both the request and response messages to use caching.

max-age indicates that the client can receive responses with a lifetime no greater than the specified time (in seconds).

min-fresh indicates that the client can receive responses with a response time less than the current time plus the specified time.

max-stale indicates that the client can receive response messages beyond the timeout period. If you specify a value for max-stale messages, the client can receive response messages that exceed the specified value of the timeout period.

Related recommendations: "Python Video Tutorial"

Date header field

The Date header field indicates the time when the message is sent. The description format of time is defined by rfc822. For example, Date:Mon,31Dec200104:25:57GMT. The time described by Date represents world standard time. To convert it to local time, you need to know the user's time zone.

Pragma header field

Pragma header field is used to contain implementation-specific instructions. The most commonly used one is Pragma:no-cache. In the HTTP/1.1 protocol, its meaning is the same as Cache-Control:no-cache.

Request message

The first line of the request message is in the following format:

MethodSPRequest-URISPHTTP-VersionCRLFMethod represents the method completed for the Request-URI, This field is case-sensitive and includes OPTIONS, GET, HEAD, POST, PUT, DELETE, and TRACE.

Methods GET and HEAD should be supported by all common WEB servers, implementation of all other methods is optional.

The GET method retrieves the information identified by Request-URI.

The HEAD method also retrieves the information identified by the Request-URI, but it does not return the message body when responding.

The POST method can request the server to receive the entity information contained in the request, and can be used to submit forms and send messages to news groups, BBS, mail groups and databases.

SP means space. Request-URI follows the URI format. When this field is an asterisk (*), it indicates that the request is not for a specific resource address, but for the server itself. HTTP- Version indicates the supported HTTP version, such as HTTP/1.1. CRLF represents the carriage return character. Request header fields allow the client to pass additional information about the request or about the client to the server.

The request header field may contain the following fields: Accept, Accept-Charset, Accept-Encoding, Accept-Language, Authorization, From, Host, If-Modified-Since, If-Match, If-None-Match, If-Range , If-Range, If-Unmodified-Since, Max-Forwards, Proxy-Authorization, Range, Referer, User-Agent.

The extension of the request header field requires support from both communicating parties. If there is an unsupported request header field, it will generally be processed as an entity header field.

Typical request message:

GET http://download.google.com/somedata.exe 
Host: download.google.com 
Accept:/ 
Pragma: no-cache 
Cache-Control: no-cache 
Referer: http://download.google.com/ 
User-Agent:Mozilla/4.04en 
Range:bytes=554554-

The first line of the above example indicates that the HTTP client (maybe a browser or downloader) obtains the specified URL through the GET method document. The brown part represents the request header field information, and the green part represents the general header part.

Host header field

The Host header field specifies the Internet host and port number of the requested resource, and must indicate the location of the original server or gateway of the requested URL. HTTP/1.1 requests must include the host header field, otherwise the system will return with a 400 status code.

Referer header field

The Referer header field allows the client to specify the source resource address of the request URI. This allows the server to generate a fallback list, which can be used to log in and optimize the cache. wait. It also allows abandoned or faulty connections to be tracked for maintenance purposes. If the requested url does not have its own url address, the Referer cannot be sent. If a partial url address is specified, this address should be a relative address.

Range header field

The Range header field can request one or more sub-ranges of the entity. For example,
represents the first 500 bytes: bytes=0-499
represents the second 500 bytes: bytes=500-999
represents the last 500 bytes: bytes=-500
Indicates the range after 500 bytes: bytes=500-
The first and last byte: bytes=0-0,-1
Specify several ranges at the same time: bytes=500-600,601-999

But the server can ignore this request header. If the unconditional GET contains the Range request header, the response will be returned with status code 206 (PartialContent) instead of 200 (OK).

User-Agent header field

The content of the User-Agent header field contains the information of the user who made the request.

Response message

The first line of the response message is in the following format:

HTTP-VersionSPStatus-CodeSPReason-PhraseCRLF

HTTP -Version indicates the supported HTTP version, such as HTTP/1.1. Status-Code is a three-digit result code. Reason-Phrase provides a simple text description for Status-Code. Status-Code is mainly used for automatic machine identification, and Reason-Phrase is mainly used to help users understand. The first number of Status-Code defines the category of the response, and the last two numbers have no classification effect. The first number may take 5 different values:

1xx: Information response class, indicating that the request is received and processing continues

2xx: Processing success response class, indicating that the action is successfully received, Understanding and acceptance

3xx: Redirect response class, in order to complete the specified action, further processing must be accepted

4xx: Client error, the customer request contains a syntax error or cannot be executed correctly

5xx: Server error, the server cannot correctly execute a correct request

The response header field allows the server to pass additional information that cannot be placed in the status line. These fields mainly describe server information and Request-URI. Further information. The response header fields include Age, Location, Proxy-Authenticate, Public, Retry-After, Server, Vary, Warning, and WWW-Authenticate. The extension of the response header field requires support by both communicating parties. If there is an unsupported response header field, it will generally be processed as an entity header field.

Typical response message:

HTTP/1.0200OK 
Date:Mon,31Dec200104:25:57GMT 
Server:Apache/1.3.14(Unix) 
Content-type:text/html 
Last-modified:Tue,17Apr200106:46:28GMT 
Etag:”a030f020ac7c01:1e9f” 
Content-length:39725426 
Content-range:bytes554554-40279979/40279980

The above is the detailed content of What does header mean in python?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn