Python--web Frame

Source: Internet
Author: User

First, the HTTP protocol

Hypertext Transfer Protocol (Http,hypertext Transfer Protocol) is one of the most widely used network protocols on the Internet. All WWW documents must comply with this standard. HTTP was originally designed to provide a way to publish and receive HTML pages. 1960 American Ted Nelson conceived a way to process text messages through a computer called hypertext (hypertext), which has become the foundation of the HTTP Hypertext Transfer Protocol Standard architecture. The Ted Nelson organization coordinated the World Wide Web Association (Wide) and the Internet Engineering Working Group (Internet Engineering Task Force) to work together to study and eventually release a series of RFCs, of which the famous RFC 2616 defines the HTTP 1.1.

Main 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 Content-type.

3. No connection: The meaning of no connection is to limit the processing of only one request per connection. When the server finishes processing the customer's request and receives the customer's answer, the connection is disconnected. In this way, the transmission time can be saved.

4. Stateless: The HTTP protocol is a stateless protocol. Stateless means that the protocol has no memory capacity for transactional processing. A lack of state means that if the previous information is required for subsequent processing, it must be re-routed, which may cause the amount of data to be transferred per connection to increase. On the other hand, it responds faster when the server does not need the previous information.
5, support B/S and C/s mode.

How HTTP Works

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.

The following are the steps for HTTP request/Response:

1. Client connects to 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.

For example: Type the URL in the browser address bar and press ENTER to experience the following process:

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;

URL Introduction

Example URL:

Http://www.aspxfans.com:8080/news/index.asp?boardID=5&ID=24618&page=1#name

1. Part of the agreement: the protocol portion of the URL is "http:", which means that the Web page uses the HTTP protocol. You can use multiple protocols in the Internet, such as http,ftp, and so on, in this case the HTTP protocol. "//" after "HTTP" is a delimiter

2. Domain name part: The domain name portion of the URL is "www.aspxfans.com". A URL, you can also use the IP address as the domain name

3. Port section: followed by the domain name is the port, between the domain name and the port using ":" As the delimiter. The port is not a required part of the URL and if the port portion is omitted, the default port will be used

4. Virtual Directory part: From the first "/" after the domain name to the last "/", is the virtual directory part. The virtual directory is also not a required part of the URL. The virtual directory in this example is "/news/"

5. File name part: From the last "/" after the domain name to "?" "So far, is the file name part, if there is no"? ", then from the domain name after the last"/"Start to" # "so far, is the document part, if not"? "and" # ", then from the last"/"after the domain name to the end, is the file name section. The file name in this example is "index.asp". The file name section is also not a required part of the URL, and if omitted, the default file name is used

6. Anchor part: From the beginning of "#" to the end, are the anchor parts. The anchor section in this example is "name". The anchor part is also not a required part of the URL

7. Parameters section: from "? The part between start and # is the parameter part, also called the search section, the query part. In this example, the parameter section is "Boardid=5&id=24618&page=1". Parameters can be allowed to have more than one parameter, with "&" as the delimiter between parameters and parameters.

HTTP response Protocol

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.

 http/1.1 Okdate:fri, May 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.

Response status?

The status code consists of three digits, and the first number defines the category of the response, divided into five categories:
1XX: Indication information--Indicates that the request has been received and continues processing
2XX: Success-Indicates that the request has been successfully received, understood, accepted
3XX: Redirect--further action is required 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 succeeded
Bad Request//client requests have syntax errors and cannot be understood by the server
Unauthorized//Request unauthorized, this status code must be used with the Www-authenticate header field
Forbidden//server receives request, but refuses to provide service
Not Found//request resource not present, eg: Wrong URL entered
Internal Server error//server unexpected errors
Server unavailable//servers are currently unable to process client requests and may return to normal after a period of time

Get and POST request differences

GET request 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 Note that the last line is a blank line POST request 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.

Ii. Web applications and web frameworks

Web Apps

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

Import socketdef handle_request (client):    buf = Client.recv (1024x768)    client.send ("http/1.1-ok\r\n\r\n". Encode ("UTF8"))    client.send ("<style= ' color:red '> Hello, Yuan</H1>". Encode (" UTF8 ") def Main ():    sock = Socket.socket ( Socket.af_inet, Socket. SOCK_STREAM)    sock.bind ((' localhost ', 8001))    Sock.listen (5)    while True:        connection, address = Sock.accept ()        handle_request (connection)        connection.close () if __name__ = = ' __main__ ':    main ()

The simplest Web application is to first save the HTML file, with a ready-made HTTP Server software, to receive user requests, from the file to read HTML, return.

If you want to generate HTML dynamically, you need to implement the above steps yourself. However, accepting HTTP requests, parsing HTTP requests, and sending HTTP responses are all menial jobs, and if we're writing these underlying code ourselves, it's going to take months to read the HTTP specification before we start writing Dynamic HTML.

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.

Three, bootstrap framework application

Bootstrap, from Twitter, is currently the most Popular front-end framework. Bootstrap is based on HTML, CSS, JAVASCRIPT, it is simple and flexible, making Web development faster.

This tutorial will teach you the basics of the Bootstrap framework, and by learning about it, you will be able to easily create WEB projects. Tutorials are divided into BOOTSTRAP basic structure, Bootstrap CSS, Bootstrap layout components, and Bootstrap plug-in sections. Each section contains simple, useful instances related to the topic.

Bootstrap is a front-end framework for rapid development of Web applications and Web sites. Bootstrap is based on HTML, CSS, and JAVASCRIPT.

Contents of the Bootstrap package
    • basic Structure : Bootstrap provides a basic structure with a grid system, a link style, and a background. This will be in &NBSP; BOOTSTRAP Basic Structure   part of the detailed explanation.
    • CSS : Bootstrap comes with the following features: Global CSS settings, defining basic HTML element styles, extensible class, and an advanced grid system. This will be in &NBSP; Bootstrap CSS
    • component : Bootstrap contains more than 10 reusable components for creating images, drop-down menus, navigation, warning boxes, pop-up boxes, and so on. This will be explained in detail in the &NBSP; layout Components section.
    • JavaScript plug-in : Bootstrap contains more than 10 custom JQuery plugins. You can include all of the plugins directly, or you can include them individually. This will be in &NBSP; Bootstrap plug-in The span class= "Apple-converted-space" >  sections are explained in detail.

       

    • Custom : You can customize Bootstrap components, less variables, and jQuery plugins to get your own version

Python--web Frame

Related Article

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.