Introduction to HTTP protocol http
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.
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 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.
HTTP Request Protocol
The request agreement complies with 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, that 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! In the HTTP protocol, there are many request methods for requests, the most common of which is GET and POST.
GET Request
Get/562f25980001b1b106000338.jpg http/1.1host img.mukewang.comuser-agent mozilla/5.0 (Windows NT 10.0; WOW64) applewebkit/537.36 (khtml, like Gecko) chrome/51.0.2704.106 safari/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, the host requests the Web server domain name Address 2, the User-agenthttp client runs the browser type details. With this header information, the Web server can determine the type of browser for the HTTP request client. 3, accept specifies the content type that the client can receive, the order of the content type indicates the order that the customer receives 4, Accept-lanuage specifies the language that the HTTP client browser uses to display the preferred choice of return information 5, 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 client browser can accept the character encoding set 7, Content-type displays the content type submitted by this HTTP request. This property is typically only required for post submission. There are two encoding types for the Content-type property value: (1) "application/x-www-form-urlencoded": the type of encoding the form data is submitted to the server, The default defaults are "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 both send text data and support binary data upload. You can use "application/x-www-form-urlencoded" when committing to form data, and when you commit a file, you need to use the "multipart/form-data" encoding type. 8. Keep-alive Indicates whether a persistent connection is required. If the Web server side sees the value here as "keep-alive", or if the request uses an HTTP 1.1 (HTTP 1.1 defaults to persistent connection), it can take advantage of the persistent connection
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
The first part: The request line, the first line is the POST request, and the http1.1 version.
The second part: The request head, the second line to the sixth line.
Part Three: blank line, blank line in line seventh.
Part IV: Request data, line eighth.
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 response message format. jpg
Example
http/1.1, Okdate:fri 06:07:21 gmtcontent-type:text/html; charset=utf-8
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 useThe 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 CodeThe 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 code: $ OK //Client Request success request//client requests have syntax errors 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 //The server receives the request, but refuses to provide service 404 not Found//request resource does not exist, eg: incorrect URL500 Internal server error// Server Unexpected error 503 Server unavailable //servers are currently unable to process client requests and may return to normal after a period of time
The difference between get and post requestsGet 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
As a result, the data submitted by get is displayed in the Address bar, while the post is submitted, the address bar does not change2, 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
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.
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.
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.
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 Web applicationFor 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 ("
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.
WsgirefFrom Wsgiref.simple_server import make_serverdef application (environ, start_response): start_response (' OK ', [ (' Content-type ', ' text/html ')]) return [b '
DIY A web framework of your ownmanage.py
From wsgiref.simple_server import make_server# request responsefrom app01.views import *from app01 Import Urlsdef routers (): urlpattern=urls. Urlpattern return urlpatterndef Applications (environ,start_response): path=environ.get ("Path_info") Print ("path", path) start_response (' OK ', [(' Content-type ', ' text/html '), (' Charset ', ' UTF8 ')]) Urlpattern=routers () Func=none for item in Urlpattern: if path==item[0]: func=item[1] break if func: return [func (environ)] else: return [B "
URLs
From app01.views Import *urlpattern = ( ("/login/", login),)
Views
Import pymysqlfrom urllib.parse Import parse_qsdef Login (Request): If Request.get ("request_method") = = "POST": pri NT ("+++++", request) #当请求方式是GET时 # user_union,pwd_union=request.get ("Query_string"). Split ("&") # _ , User=user_union.split ("=") # _,pwd=pwd_union.split ("=") # environment variable content_length may be null or the value is missing try: request_body_size = Int (request.get (' Content_length ', 0)) except (ValueError): request_body_size = 0 # When the request method is post, the variable is placed in the HTTP request information that exists in the domain Wsgi.input file and sent by the WSGI server. Request_body = request[' Wsgi.input '].read (request_body_size) d = Parse_qs (request_body) user=d.get (b "User") [ 0].decode ("UTF8") Pwd=d.get (b "pwd") [0].decode ("UTF8") print ("User", user,pwd) #连接数据库 conn = Pym Ysql.connect (host= ", port= 3306,user = ' root ', passwd= ', db= ' S6 ') #db: library name #创建游标 cur = conn.cursor () SQL = "SELECT * from Userinfo2 WHERE NAME = '%s ' and PASSWORD = '%s '"%(user,pwd) Cur.execute (SQL) if Cur.fetchone (): F=open ("templates/backend.html", "RB") d Ata=f.read () data= (Data.decode ("UTF8"))%user return Data.encode ("UTF8") Else:ret Urn B "User or PWD is wrong" else:f = Open ("templates/login.html", "RB") data = F.read () f.close () Return data
Models
Import Pymysqlimport pymysql# Connection Database conn = Pymysql.connect (host= ", port= 3306,user = ' root ', passwd= ', db= ' S6 ') #db: library name # Create cursor cur = conn.cursor () # sql= "# CREATE TABLE Userinfo2 (# ID INT PRIMARY KEY, # name VARCHAR (+), # password VARCHAR (+) # # # # # Cur.execute (SQL) # # Cur.executemany ("INSERT into Userinfo2 values (%s,%s,%s)", [(1, "Yuan", "123"), # (2, "Alex", "456"), # (3, "Egon", "789")]) Cur.execute ("select * from Userinfo2 WHERE name= ' yuan ' and PASSWORD = ' 123 ' ") #提交conn. Commit () #关闭指针对象cur. Close () #关闭连接对象conn. Close ()
Python Web framework "supplemental" Custom web Framework