Talking about HTTP protocol

Source: Internet
Author: User

1.http recognition
    • The HTTP protocol is an abbreviation for the Hyper Text Transfer Protocol (Hypertext Transfer Protocol) and is the most widely used network protocol on the Internet for the transfer of hypertext data between Web servers and browsers.

    • HTTP is a TCP/IP communication protocol to pass data (HTML Web files, image files, video files, query results, etc.)

      HTML: (Hyper Text mark-up Language) Hypertext Markup Language, HTML to write Web pages

    • In layman's terms: HTTP is the protocol that transmits HTML pages over the network, and is used for browser and server communication.

2. Request/response (Request/response) model of the HTTP protocol

3. HTTP Request Message Format analysis

Here is the sample data we want to request:

 get/index.html http/1.1host:  192.168.192.221:8080 connection:keep -alivecache -control: Max-age=0upgrade -insecure-requests:1user -agent:mozilla/5.0 (Windows NT 10.0; WOW64) applewebkit/537.36 (khtml, like Gecko) chrome/65.0.3325.162 safari/537.36accept: Text /html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8 accept -encoding:gzip, deflateaccept - Language:zh-cn,zh;q=0.9cookie:baiduid  =8fd6ed496a03231d920484c6379517cf:fg= 1; 

1.GET / HTTP/1.1 叫做请求行. 里面包含3个信息, 以空格隔开
2.请求头. 除了第一行之外, 剩下的所有数据的格式都是类似的.叫请求头。
Request Message Format Summary

4. HTTP Response Message Format analysis
http/1.1 okconnection:keep-alivecontent-encoding:gzipcontent-type:text/html; charset=utf-8date:wed, Mar 2018 09:52:48 gmtserver:bws/1.1
1. The first line of http/1.1 is called the response line, divided into 3 parts, the first part http/1.1 represents the version of the HTTP protocol, the second part is a number, this number indicates the response status code, the user sent a request to the server, if the server returns the response message, then the status code Usually 200, the third part of the "OK" represents a reason phrase, which represents a simple description of the preceding status code. What needs to be said here is that the status code of the response, in addition to 200, has other status codes. (Other status code own Baidu)2. The second line below all the contents, we call the response header.Response Data Format Summary

 
The process by which a browser accesses a website

The basic flow is as follows:

    1. The user enters the URL.
    2. The browser requests the DNS server to obtain the IP address of the domain name.
    3. Request to connect to the IP address server.
    4. Send a resource request. (HTTP protocol)
    5. The Web server receives the request and parses the request to determine the user's intent.
    6. Gets the resource that the user wants.
    7. Returns the resource to the HTTP server program.
    8. The HTTP server program sends the resource data over the network to the browser.
    9. The browser parses the data that renders the request.
The entire process of TCP communication, such as:

1. TCP Short Connection

To simulate a TCP short connection scenario:

    1. Client initiates connection request to server
    2. server receives a request, both parties establish a connection
    3. Client sends messages to server
    4. Server responds to Client
    5. Once read and write completed, at which point both sides can initiate a close operation

In step 5, it is common for the client to initiate the close operation first. Of course, there are no special circumstances to rule out.

From the above description, the short connection will generally only pass a read and write operation between Client/server!

2. TCP Long Connection

To simulate a case of a long connection:

    1. Client initiates a connection to the server
    2. server receives a request, both parties establish a connection
    3. Client sends messages to server
    4. Server responds to Client
    5. Once read and write completed, the connection does not close
    6. Subsequent read and write operations ...
    7. Client initiates a shutdown request after a long time operation

3. TCP/IP protocol (family)

The Internet Protocol contains hundreds of protocol standards, but the most important two protocols are TCP and IP protocol, so everyone put the Internet Protocol referred to as TCP/IP protocol (family)

Common network protocols are as follows:

    1. Network interface layer (physical layer, Data link layer): Including transmission media (network cable), computer corresponding to the interface card, etc., in fact, this layer of TCP/IP protocol is not defined, to its upper "network layer" provides access interface.
    2. Network layer (Internet layer): The main IP address to complete the addressing of the host, it is also responsible for the packet in a variety of network routing
    3. Transport Layer: Provides end-to-end communication primarily for applications on two hosts.
    4. Application layer: Provide users with the services they need, such as HTTP service, FTP service, SMTP service, etc.
1. Simulate browser access to the server

import socketdef Main (): # 1. Create a TCP Client socket object Http_client_socket = Socket.soc Ket (socket.af_inet, socket. SOCK_STREAM) # 2. Connect the server side Http_client_socket.connect ((' 127.0.0.1 ', 7788)) # 3. Send HTTP Request message Format "" "1. Request Line: get/index.html http/1.1 2. Request Header: host:127.0.0.1:7788 3. Delimiter ' \ r \ n ' 4. Request body product_id=1001 "" "Request_line = ' get/index.html http/1.1\r\n ' # request line, must have request_headers = ' host:127.0 .0.1:7788\r\n ' # request header request_headers + = ' accept:text/html\r\n ' split = "\ r \ n" # request header with request body delimiter request_body = "Pro duct_id=1001\r\n "request_data = request_line + request_headers + split + request_body # splicing request message Data # Send request data to the server, before sending Encoding Http_client_socket.send (Request_data.encode ("Utf-8")) # waits to accept a message answered from the server Response_data = HTTP_CLIENT_SOCKET.RECV ( 1024x768) Print ("HTTP service-side answered data:", Response_data.decode ()) # decode output # Close socket Http_client_socket.close () if __name__ = = ' __ Main__ ': Main () 

  

2. Simple HTTP Server implementation
1 ImportSocket2 3 4 defhandle_client (client_socket):5     """handling requests from browser clients"""6 7     #waiting for messages sent by the receiving client8Recv_data = CLIENT_SOCKET.RECV (4096)9 Ten     #Decoding Data OneRequest_data = Recv_data.decode ("Utf-8") A  -     #display the received request message data -     Print(request_data) the  -     #reply to client in HTTP response message format -     """HTTP Response message Format - 1. Response line: http/1.1 OK + 2. Response Head server:mimiweb1.0 connection:keep-alive - 3. separators \ r \ n + 4. Response body Very good A     """ atResponse_line ="http/1.1 ok\r\n"  #response line, must have -Response_headers ="server:mimiweb1.0\r\n" -Response_headers + ="connection:keep-alive\r\n" -Split ="\ r \ n"  #delimiter for request header and request body -Response_body ="very good\r\n" -  in     #splicing Response message data -Response_datas = response_line + response_headers + split +response_body to  +     #sending response message data to the client -Client_socket.send (Response_datas.encode ("Utf-8")) the  *     #Close Socket $ client_socket.close ()Panax Notoginseng  -  the defMain (): +     """Program Master Control Portal""" A  the     #creating a listening socket +Http_server_socket =Socket.socket (socket.af_inet, socket. SOCK_STREAM) -  $     #when the socket is waved four times, the address port can be reused immediately $ http_server_socket.setsockopt (socket. Sol_socket, SOCKET. SO_REUSEADDR, True) -  -     #server-side bound Port theHttp_server_socket.bind (("', 7788)) - Wuyi     #Turn on monitoring theHttp_server_socket.listen (128) -  Wu     #waiting to accept requests from browser clients -      whileTrue: AboutClient_socket, client_addr =http_server_socket.accept () $         Print("There are new client requests, from >>>", CLIENT_ADDR) -  -         #servicing a client in a function - handle_client (Client_socket) A  +  the if __name__=='__main__': -Main ()
Server-side

Client

Talking about HTTP protocol

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.