Http protocol and Web framework

A, http protocol Introduction

  HTTP protocol is the Hyper Text Transfer abbreviation Protocol (Hypertext Transfer Protocol) is used for the World Wide Web (WWW: World Wide Web) transmission hypertext transfer protocol between the server and local browser.

  HTTP is a protocol belonging to the object-oriented application layer, due to its simple, fast way for distributed hypermedia information system. It is proposed in 1990, after several years of use and development, has been continuously improved and expanded. HTTP protocol works on the client - server architecture on. Browser as an HTTP client URL that is WEB server sends all requests to the server via HTTP. Web server according to the received request, transmits the response information to the client.

Two, http protocol features

2.1 based on tcp / ip protocol application layer protocol

2.2 based on the request - response pattern

  HTTP protocol that a request sent from the client, the server responds to the last request and returns. In other words, definitely start start establishing communications client, the server does not send a response until a request is received

2.3 Stateless save

  HTTP is a non-save state, i.e., no state (Stateless) protocol. HTTP protocol itself does not request and the communication status between the response stored. That this level in the HTTP, a protocol for sending requests or responses had nothing lasting treatment.

   Using the HTTP protocol, whenever a new request is sent, it will produce a corresponding new response. The agreement itself does not keep everything before the request or response information packet. This is to handle a large number of transactions quickly, to ensure that the protocol scalability, and the HTTP protocol specifically designed to be so simple. However, as the Web continues to develop, because there is no state which led to business processing becomes difficult cases increased. For example, a user logs on to a shopping site, even after he jumps to the other page of the station, but also need to be able to continue to stay logged in. For this example, the site is to be able to grasp who sent the request, the need to preserve the user's status. HTTP / 1.1 is a stateless protocol, although, in order to realize a desired function to maintain state, then the introduction of Cookie technology. With Cookie and then HTTP protocol communication, you can manage the state. For more information about Cookie explain later

2.4 No connection

  Meaning No connection is to limit the processing only one request per connection. After the server processes client requests and receives the customer's response, i.e., disconnected. In this way it can save transmission time.

Three, http protocol request and response protocol

  http protocol comprising protocol data requested by the browser sends a request to the protocol sent by the server to follow the data server to the browser to follow. HTTP is believed to be as HTTP packets. Requester (client) HTTP packets do request packet, the responder (server side) made in response message. HTTP word text message itself is composed of multiple rows of data.

Request Protocol

  Request format:

 

 

 Request method: get and post requests

  • GET data is placed after the submitted URL to? URL and transmit the divided data, is connected to between & parameter, such as EditBook? Name = test1 & id = 123456. POST method is to package the data submitted in a HTTP request body. 
  • GET submitted the data size is limited (because the browser restrictions on the length of the URL), and the data submitted by the POST method is not limited.
  • GET and POST requests the server request data acquired in different ways.

 

    GET request
     # request the first line of 
    GET / HTTP / 1.1 \ r \ the n-
     # GET request parameters behind the 
    GET / name = LQZ & Age = 18 HTTP / 1.1? \ R \ the n-
     # request header 
    Host: 127.0.0.1:8008 \ r \ the n- 
    Connection: Keep - Alive \ R & lt \ n- 
    the Cache -Control: Age-max = 0 \ R & lt \ n- 
    Upgrade -Insecure-Requests:. 1 \ R & lt \ n- 
    the User -Agent: the Mozilla / 5.0 (the Windows NT 6.1; Win64; x64-) AppleWebKit /537.36 (KHTML, like the Gecko) the Chrome / 65.0.3325.181 Safari / 537.36 \ R & lt \ n- 
    the Accept: text /html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/ *; q = 0.8 \ r \ nAccept-Encoding: the gzip, the deflate, br \ R & lt \ n- 
    the Accept - Language: the CN-ZH, ZH; 0.9 = Q \ R & lt \ n- 
    cookies: csrftoken = 7xx6BxQDJ6KB0PM7qS8uTA892ACtooNbnnF4LDwlYk1Y7S7nTS81FBqwruizHsxF \ R & lt \ n-\ R & lt \ n- '
     # request body (get request, the request body is empty)    
    '' ' ' 
    '' 
    the POST request 
    # request the first row 
    the POST /? name = & LQZ Age = 18 is the HTTP / 1.1 \ R & lt \ n-
     # request header
    Host: 127.0.0.1:8008\r\nConnection: keep-alive\r\nContent-Length: 21\r\nCache-Control: max-age=0\r\nOrigin: http://127.0.0.1:8008\r\nUpgrade-Insecure-Requests: 1\r\nContent-Type: application/x-www-form-urlencoded\r\nUser-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36\r\nAccept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8\r\nReferer: http://127.0.0.1:8008/?name=lqz&age=18\r\nAccept-Encoding: gzip, deflate, br\r\nAccept-Language: zh-CN,zh;q=0.9\r\nCookie: csrftoken=7xx6BxQDJ6KB0PM7qS8uTA892ACtooNbnnF4LDwlYk1Y7S7nTS81FBqwruizHsxF\r\n\r\n
    # 请求体
    name=lqz&password=123'
    
GET and POST requests

 

 Response protocol

  Response format:

The response status code

  状态码的职 是当客户端向服务器端发送请求时, 返回的请求 结果。借助状态码,用户可以知道服务器端是正常 理了请求,还是出 现了 。状态码如200 OK,以3位数字和原因 成。数字中的 一位指定了响应 别,后两位无分 。响应 别有以5种。

 

四、Web框架

  Web框架(Web framework)是一种开发框架,用来支持动态网站、网络应用和网络服务的开发。这大多数的web框架提供了一套开发和部署网站的方式,也为web行为提供了一套通用的方法。web框架已经实现了很多功能,开发人员使用框架提供的方法并且完成自己的业务逻辑,就能快速开发web应用了。浏览器和服务器的是基于HTTP协议进行通信的。也可以说web框架就是在以上十几行代码基础张扩展出来的,有很多简单方便使用的方法,大大提高了开发的效率。

wsgiref模块

  最简单的Web应用就是先把HTML用文件保存好,用一个现成的HTTP服务器软件,接收用户请求,从文件中读取HTML,返回。

  如果要动态生成HTML,就需要把上述步骤自己来实现。不过,接受HTTP请求、解析HTTP请求、发送HTTP响应都是苦力活,如果我们自己来写这些底层代码,还没开始写动态HTML呢,就得花个把月去读HTTP规范。

  正确的做法是底层代码由专门的服务器软件实现,我们用Python专注于生成HTML文档。因为我们不希望接触到TCP连接、HTTP原始请求和响应格式,所以,需要一个统一的接口协议来实现这样的服务器软件,让我们专心用Python编写Web业务。这个接口就是WSGI:Web Server Gateway Interface。而wsgiref模块就是python基于wsgi协议开发的服务模块。

from wsgiref.simple_server import make_server

def mya(environ, start_response):
    print(environ)
    start_response('200 OK', [('Content-Type', 'text/html')])
    if environ.get('PATH_INFO') == '/index':
        with open('index.html','rb') as f:
            data=f.read()

    elif environ.get('PATH_INFO') == '/login':
        with open('login.html', 'rb') as f:
            data = f.read()
    else:
        data=b'<h1>Hello, web!</h1>'
    return [data]

if __name__ == '__main__':
    myserver = make_server('', 8011, mya)
    print('监听8010')
    myserver.serve_forever()
复制代码
wsgiref简单应用

 

Guess you like

Origin www.cnblogs.com/wangchengpei/p/10981511.html