HTTP protocol and web framework

Source: Internet
Author: User

First, HTTP protocol one, HTTP introduction

The HTTP protocol is an abbreviation for the Hyper Text Transfer Protocol (Hypertext Transfer Protocol), which is used to transfer hypertext to the local browser from the World Wide Web (www:world Wide Web) server.

HTTP is a TCP/IP communication protocol that transmits data (HTML files, image files, query results, and so on).

HTTP is an object-oriented protocol belonging to the application layer, which is suitable for distributed hypermedia information System because of its simple and fast way. It was proposed in 1990, after several years of use and development, has been continuously improved and expanded. Currently used in the WWW is the sixth edition of Http/1.0, http/1.1 standardization work is in progress, and Http-ng (Next Generation of HTTP) has been proposed.

The HTTP protocol works on the client-server architecture. The browser sends all requests via URLs to the HTTP server, which is the Web servers, as an HTTP client. The Web server sends a response message to the client, based on the received request.

  

Second, HTTP features

1, simple and fast: When a customer requests a service from the server, it simply transmits the request method and path. The request method commonly has, POST. Each method specifies a different type of contact between the customer and the server.

Because the HTTP protocol is simple, the HTTP server's program size is small, so the communication speed is fast.

2, Flexible: HTTP allows the transfer of any type of data objects. The type being transmitted is marked by Conten-type.

3, no connection: The meaning of no connection is to restrict each connection to process only one request, the server finishes processing the customer's request, and receives the customer's answer, that is, disconnect. In this way, the transmission time can be saved.

4, stateless: HTTP protocol is a stateless protocol. Stateless means that there is no memory capability for transactional processing.

A lack of state means that if the previous information is required for subsequent processing, it must be re-transmitted, which may result in an increase in the amount of data sent per connection. On the other hand, when the server does not need the previous information, its response is faster.

Third, the HTTP principle

The HTTP protocol defines how Web clients request Web pages from a Web server and how the server routes Web pages to clients. The HTTP protocol uses the request/response model. The client sends a request message to the server that contains the requested method, URL, protocol version, request header, and request data. The server responds with a status line that includes the version of the Protocol, the success or error code, the server information, the response header, and the response data.

1. Step 1 of the HTTP request/response, the client connects to the Web server

An HTTP client, typically a browser, establishes a TCP socket connection with the HTTP port of the Web server (default is 80). For example, http://www.oakcms.cn.

2. Sending HTTP requests

Through TCP sockets, the client sends a text request message to the Web server, which consists of a request line, a request header, a blank line, and 4 parts of the requested data.

3. The server accepts the request and returns the HTTP response

The Web server resolves the request and locates the requested resource. The server writes a copy of the resource to the TCP socket, which is read by the client. A response consists of a status line, a response header, a blank line, and a 4 portion of the response data.

4. Release the connection TCP connection

If the connection mode is close, the server actively shuts down the TCP connection, the client shuts down the connection passively, releases the TCP connection, and if the connection mode is keepalive, the connection is maintained for a period of time and the request can continue to be received;

5. client browser parsing HTML content

The client browser parses the status line first to see the status code indicating whether the request was successful. Each response header is then parsed, and the response header informs the following character sets for several bytes of HTML documents and documents. The client browser reads the response data HTML, formats it according to the syntax of the HTML, and displays it in a browser window.

2, Application: Type the URL in the browser address bar, press ENTER after the following process will go through:

1. The browser requests the DNS server to resolve the IP address of the domain name in the URL;

2, after resolving the IP address, according to the IP address and the default port 80, and the server to establish a TCP connection;

3, the browser issued a read file (the URL in the back part of the corresponding file) HTTP request, the request message as a TCP three handshake third message data sent to the server;

4, the server responds to the browser request, and the corresponding HTML resulting sent to the browser;

5, release the TCP connection;

6, the browser will be the HTML text and display content.

Iv. HTTP Request Protocol 1, the request protocol follows the following format:
Request the first line;/        / Request Path protocol and version, for example: get/index.html http/1.1 request header information;      //  Request Header name: request Header content, This is the key:value format, for example: Host:localhost blank line;           //  used to separate the request body from the request body .         //  get no request body, only post has request body. 

The browser sends the contents of the server to this format, if not this format the server will not be able to interpret!

2. Request method

In the HTTP protocol, there are many request methods for requests, the most common of which is GET and POST.

1. Get Request
Get/562f25980001b1b106000338.jpg http/1.1Host Img.mukewang.comUser-agent mozilla/5.0(Windows NT10.0; WOW64) applewebkit/537.36(khtml, like Gecko) chrome/51.0.2704.106safari/537.36Accept Image/webp,image/*,*/*;q=0.8Referer http://www.imooc.com/accept-Encoding gzip, deflate, sdchaccept-language zh-cn,zh;q=0.8

The HTTP default request method is get
* No request body
* Data volume is limited!
* GET request data is exposed in the browser's address bar

Get requests commonly used actions:
1. Give the URL directly in the address bar of the browser, then it must be a GET request
2. Clicking on a hyperlink on the page must also be a GET request
3. When submitting a form, the form uses the GET request by default, but can be set to post

Request Header:

1, host requests the Web server domain name address2, user-agenthttp Details of the browser type that the client is running. With this header information, the Web server can determine the type of browser for the HTTP request client. 3, accept specifies the type of content that the client can receive, and the order of the content type indicates the order in which the customer is received4, accept-Lanuage Specifies the language that the HTTP client browser uses to display the return information preference5, accept-encoding specifies that the Web server that the client browser can support returns the content compression encoding type. Indicates that the server is allowed to compress the output before it is sent to the client to conserve bandwidth. The return compression format that the client browser can support is set here. 6, accept-Charsethttp the set of character encodings that the client browser can accept7, content-Type Displays the content type submitted by this HTTP request. Typically only post submissions need to set this property about content-The type attribute value has the following two types of encoding: (1) "application/x-www-form-urlencoded": the type of encoding used when the form data is submitted to the server, the default value is "application/x-www-form-urlencoded ". However, this encoding is inefficient when sending large amounts of text to the server, including text or binary data that contains non-ASCII characters. (2) "Multipart/form-data": When the file is uploaded, the type of encoding used should be "multipart/form-Data, which can send both textual and binary data uploads. When submitted as form data, you can use the application/x-www-form-urlencoded "When a file is submitted, the" Multipart/form-data "encoding type is required.
2. Post request
post/http1.1Host:www.wrox.comUser-agent:mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1;. NET CLR 2.0.50727;. NET CLR 3.0.04506.648;. NET CLR 3.5.21022) Content-type:application/x-www-form-  Urlencodedcontent-length:40connection:keep-alivename=professional%20ajax&publisher= Wiley
V. HTTP response protocol 1, Response format

In general, the server will return an HTTP response message after receiving and processing a request from the client.

The HTTP response is also made up of four parts: the status line, the message header, the blank line, and the response body.

  

2. Example
http/1.1, Okdate:fri 06:07:21 gmtcontent-type:text/html; charset=utf-8<HTML>      <Head></Head>      <Body>            <!--body goes here -      </Body></HTML>
The first part: The status line, consists of the HTTP protocol version number, the status code, the status message three parts.

The first behavior status line, (http/1.1) indicates that the HTTP version is 1.1, the status code is 200, and the status message is (OK)

Part II: Message headers that describe some additional information that the client will use

The second line and the third behavior message header,
Date: The day and time the response was generated; Content-type: The MIME-type HTML (text/html) is specified and the encoding type is UTF-8

The third part: a blank line, a blank line after the message header is required Part IV: The response body, the text information that the server returns to the client.

The HTML portion following the empty line is the response body.

3. Response Status Code

Status Code classification:

The status code consists of three digits, and the first number defines the category of the response, divided into five categories: 1xx: Indicates that the request has been received and continues processing 2xx: Success-Indicates that the request has been successfully received, understood, Accept 3xx: Redirect-A further action must be taken to complete the request 4xx: Client Error-Request syntax error or request not implemented 5XX: server-side error-the server failed to implement a legitimate request

Common Status Codes:

$ OK                        //Client request success The bad Request               //client request has a syntax error and cannot be understood by the server 401 Unauthorized              //Request Unauthorized, This status code must be used with the Www-authenticate header domain 403 Forbidden                 //server receives the request, but refuses to provide service 404                 not Found//request resource does not exist, eg: wrong URL500 entered Internal Server error     //server Unexpected error 503 server unavailable        //server is currently unable to process client requests and may recover after a period of time

 More Status code links: http://www.runoob.com/http/http-status-codes.html

4. The difference between get and post requests

Get Request: Note that the last line is a blank line

Get/books/?sex=man&name=professional http/1.1host:www.wrox.comuser-agent:mozilla/5.0 (Windows; U Windows NT 5.1; En-us; rv:1.7.6) gecko/20050225 firefox/1.0.1connection:keep-alive

POST

post/http/1.1host:www.wrox.comuser-agent:mozilla/5.0 (Windows; U Windows NT 5.1; En-us; rv:1.7.6) gecko/20050225 firefox/1.0.1content-type:application/x-www-form-urlencodedcontent-length:40connection: Keep-alivename=professional%20ajax&publisher=wiley

1, get commits, the requested data will be appended to the URL (that is, the data placed in the HTTP protocol header), to split the URL and transfer data, multiple parameters with & connection; for example: login.action?name=hyddd&password= Idontknow&verify=%e4%bd%a0%E5%A5%BD. If the data is an English letter/number, sent as is, if it is a space, converted to +, if it is Chinese/other characters, the string is directly encrypted with BASE64, such as:%E4%BD%A0%E5%A5%BD, where the xx in%xx is the symbol in 16 binary notation ASCII.

Post submission: Place the submitted data in the package of the HTTP packet. In the example above, the red font indicates the actual transfer data

Therefore, the data submitted by get is displayed in the Address bar, and the address bar does not change when the post is submitted.

2, the size of the transmitted data: first of all: the HTTP protocol does not restrict the size of the transmitted data, the HTTP protocol specification does not limit the length of the URL.

The main limitations in the actual development are:

GET: Specific browsers and servers have restrictions on URL length, such as IE's limit on URL length is 2083 bytes (2k+35). For other browsers, such as Netscape, Firefox, etc., there is theoretically no length limit, and its limitations depend on the support of the operating system.

Therefore, for a get commit, the transmitted data is limited by the URL length.

POST: The theoretical data is not limited because it is not transmitted via a URL. However, the actual Web server will be required to limit the size of the post submission data, Apache, IIS6 have their own configuration.

the difference between get and post
    1. Get submitted data is placed after the URL, to split the URL and transfer data, the parameters are connected with &, such as editposts.aspx?name=test1&id=123456. The Post method is to put the submitted data in the body of the HTTP packet.
    2. The data size for get commits is limited (because the browser has a limit on the length of the URL), and there is no limit to the data submitted by the Post method.
    3. The Get method needs to use Request.QueryString to get the value of the variable, and the Post method takes the value of the variable by Request.Form.
    4. The Get method submits the data, which brings security problems, such as a login page, when the data is submitted via get, the user name and password will appear on the URL, and if the page can be cached or someone else can access the machine, the user's account and password can be obtained from the history record.
Web application and Web framework one, Web application

For all Web applications, essentially a socket server, the user's browser is actually a socket client.

ImportSocketdefhandle_request (client): BUF= CLIENT.RECV (1024) Client.send ("http/1.1 ok\r\n\r\n". Encode ("UTF8")) Client.send ("

". Encode ("UTF8"))defMain (): Sock=Socket.socket (socket.af_inet, socket. Sock_stream) Sock.bind (('localhost', 8001)) Sock.listen (5) whiletrue:connection, Address=sock.accept () handle_request (connection) connection.close ()if __name__=='__main__': Main ()

The simplest web application is to save HTML with a file, use a thread of HTTP server software, receive user requests, read HTML from a file, and return.

To generate HTML dynamically, you need to implement the above steps yourself. However, the HTTP request is received, parsed. HTTP requests, sending HTTP responses are all menial work, if you write these low-level code, have not started to write Dynamic HTML, it took months to read the HTTP specification.

The correct approach is that the underlying code is implemented by specialized server software, and we use Python to focus on generating HTML documents. Because we don't want to be exposed to TCP connections, HTTP RAW requests, and response formats, we need a unified interface that lets us focus on writing Web services in Python.

This interface is the Wsgi:web Server Gateway Interface.

Second, Wsgiref module
 fromWsgiref.simple_serverImportMake_serverdefapplication (environ, start_response): Start_response ('OK', [('Content-type','text/html')])    return[b'']httpd= Make_server ("', 8080, application)Print('serving HTTP on port 8000 ...')#start listening for HTTP requests:Httpd.serve_forever ()
Third, web framework

manage.py

 fromWsgiref.simple_serverImportMake_server#Request Response fromApp01.viewsImport* fromApp01ImportURLsdefrouters (): Urlpattern=URLs. UrlpatternreturnUrlpatterndefApplications (environ,start_response): Path=environ.get ("Path_info")    Print("Path", Path) start_response ('OK', [('Content-type','text/html'),('Charset','UTF8')]) Urlpattern=routers () func=None forIteminchUrlpattern:ifpath==item[0]: Func=item[1]             Break    iffunc:return[func (environ)]Else:        return[b""]    #return [B "if __name__=='__main__': T=make_server ("", 8810, applications)Print("server is working ...") T.serve_forever ()

HTTP protocol and web framework

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.