A complete HTTP request process begins when the connection to the TCP three handshake is successful, and the client begins sending an HTTP request to the server in the specified format, after the server receives the request, resolves the HTTP request, processes the business logic, and returns an HTTP response to the client. The HTTP response content also has a standard format. No matter what the client or the server, as long as the HTTP protocol standards to achieve, then it must be universal.
HTTP Request Format
The HTTP request format consists of four main parts: the request line, the request header, the blank line, the message body, each part contains one row
| 123456 |
<request-line> <General-headers> <request-headers> <entity-headers> <empty-line> [<message-body>] |
Request Line : The request line is the first line of the request message and consists of three parts: the request method (Get/post/delete/put/head), the URI path of the request resource, the version number of the HTTP
| 1 |
GET /index. HTML HTTP/1.1 |
Request Header : The information in the request header has a cache-related header (cache-control,if-modified-since), Client identity information (user-agent), and so on. For example:
| 1234 |
Cache-Control:Max-Age=0 cookie:gsscrollpos = _ga=GA12.329038035.1465891024; _gat=1 If-Modified-Since:Sun, May : : GMT User-Agent:Mozilla/5.0 (windows nt 10.0; wow64) AppleWebKit /537.36 (khtml, like gecko) chrome /51.0.2704.84 Safari/537.36 |
message body : The request body is the client's request data to the server, this part of the data is not required for each request.
HTTP Response Format
The server returns an HTTP corresponding message to the client after the request has been processed. The format of the HTTP response message includes: status line, response header, blank line, message body. Each part contains one row.
| 123456 |
<status-line> <General-headers> <response-headers> <entity-headers> <empty-line> [<message-body>] |
status line : The status line is located in the first line of the corresponding message, with the HTTP protocol version number, status code and status description three parts. Such as:
Response Headers : The response header is some information that the server passes to the client to describe the server and the policy when it continues to access the resource in the future.
| 123456 |
Connection:Keep-alive Content-Encoding:gzip content-type:text/html; charset=utf-8 Date:Fri, June : GMT Server:nginx/1.9.12 Transfer-Encoding:chunked |
Response Body: The response body is the HTML text content returned to the client by the server, or data in other formats, such as video streams, pictures, or audio data.
Socket
Web server is based on socket programming, also known as Network programming, network protocol through a socket called the object abstraction, socket can establish network connection, read data, write data. The socket module defines a number of constant parameters that specify the address family of the socket, the type of socket, and the supported TCP/IP protocols.
Socket.socket ([family[, type[, Proto]]): Creates a socket object based on the specified address family and socket type, protocol number (default = 0). Af_inet corresponds to the IPV4, Af_inet6 corresponds to the IPV6.
Socket Object method
- Socket.bind (address): BIND IP addresses and ports
- Socket.listen: Start listening on the specified port, the backlog represents the maximum length of the connection queue
- Socket.setblocking (flag): set to a non-blocking or blocked socket, if it is non-blocking, then call recv if there is no data readable, then long to directly return an error, if set to block mode, if there is no data readable, Then it is stuck in the state of blocking waiting for data.
- Socket.accept (): When a connection request comes in, receives the connection and returns a socket object that can send and receive data based on the connection.
- Socket.sendall (string[, flags]): Send data
- SOCKET.RECV (bufsize[, Flags]): Receive data
- Socket.close (): Closes the socket connection.
After the HTTP specification and the socket are clear, we can use the socket to implement a simple HTTP server. Code:
| 12345678910111213141516171819 |
#-*-Coding:utf-8-*-Import Socket if __name__ = = ' __main__ ': PORT = 8000 sock = socket.socket (socket.af_inet, socketsock_stream) sock. Bind((' 127.0.0.1 ', PORT)) sock. Listen(1) print ' serving HTTP on port%s ... ' % Port While 1: Conn, addr = sock. Accept() Print conn, addr request = conn. Recv(1024x768) # HTTP response message response = "http/1.1 Ok\ncontent-type:text/html\nserver:myserver\n\nhello, World !" Conn. Sendall(response) Conn. Close() |
Browser Access address: http://localhost:8000
http://blog.jobbole.com/106632/
A complete HTTP request process