HTTP stands for Hyper Text Transfer Protocol. Its development was the result of a collaboration between the World Wide Web Consortium and the Internet Engineering Task Force (IETF), which eventually published a series of RFCS, RFC 1945 defines HTTP/1.0. The most famous of these is RFC 2616. RFC 2616 defines a version that is commonly used today, HTTP 1.1.
HyperText Transfer Protocol (HTTP) is used to Transfer HyperText from WWW server to local browser. It can make browsers more efficient and reduce network traffic. It not only ensures that the computer transfers the hypertext document correctly and quickly, but also determines which parts of the document to transfer and which parts of the content to display first (e.g. text before graphics).
- HTTP is a TCP/ IP-based communication protocol to transfer data (HTML files, image files, query results, etc.).
- HTTP is an application layer protocol, composed of requests and responses, and is a standard client-server model.
- HTTP is a stateless protocol.
! Upload thoroughly understand HTTP protocol. JPG failed. Please try again.
- CREATE :PUT
- READ: GET
- UPDATE: the POST
- DELETE: DELETE
HTTP/1.1 200 OK 200 indicates a successful response. The following OK indicates a description. HTTP/1.1 200 OK indicates a successful response. If something other than 200 is returned, there are often other functions, such as
- Failed response 404 Not Found: Page does Not exist
- 500 Internal Server Error: Indicates an Internal Server Error
- . And so on…
-
Each HTTP request and response follows the same format, with an HTTP containing both Header and Body parts, where the Body is optional.
-
The HTTP protocol is a text protocol, so its format is very simple. HTTP GET request format: GET /path HTTP/1.1 Header1: Value1 Header2: Value2 Header3: Value3 One for each Header with the newline character \r\n or os.linesep HTTP POST request format: POST/PATH HTTP/1.1 Header1: Value1 Header2: Value2 Header3: Value3
body data goes here… When two consecutive \r\n are encountered, the Header part ends and all the data following it is Body. The format of the HTTP response is 200 OK Header1: Value1 Header2: Value2 Header3: Value3
body data goes here… HTTP responses containing body are also delimited by \r\n\r\n. Note again that the data Type for Body is determined by the Content-Type header. In the case of a Web page, Body is text, and in the case of an image, Body is the binary of the image. When content-encoding is present, the Body data is compressed. The most common compression method is gzip, so when seeing Content-Encoding: gzip, the Body data needs to be uncompressed to get the real data. The purpose of compression is to reduce the size of the Body and speed up network transmission. #4Web static server ##1. Display fixed pages
import socket
import multiprocessing
import os
import time
def serverHandler(clientSocket, clientAddr):
'Interact with the requesting client'
Receive messages from clients
recvData = clientSocket.recv(1024).decode('utf-8')
print(recvData)
The server sends a message to the client in response
responseLine = 'the HTTP / 1.1 200 OK' + os.linesep
responseHeader = 'Server: laowang' + os.linesep
responseHeader += 'Date: %s' % time.ctime() + os.linesep
responseBody = 'Less than six feet.'
sendData = (responseLine + responseHeader + os.linesep + responseBody).encode('gbk')
clientSocket.send(sendData)
# close
clientSocket.close()
def main():
'Program entry'
# socket object
serverSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# bind port number, can reuse the port number
#serverSocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
# binding
serverSocket.bind((' ', 8000))
# to monitor
serverSocket.listen()
while True:
# to receive
clientSocket, clientAddr = serverSocket.accept()
print(clientSocket)
Start a new process and perform the interaction
multiprocessing.Process(target=serverHandler, args=(clientSocket, clientAddr)).start()
Close the client object
clientSocket.close()
if __name__ == '__main__':
main()
Copy the code
##2. Display the desired page
import time,multiprocessing,socket,os,re
G_PATH = './html'
def serveHandler(clientSocket,clientAddr):
recvData = clientSocket.recv(1024).decode('gbk')
print(recvData)
lineFirst = recvData.splitlines()[0]
strFirst = re.split(r'+',lineFirst)
fileName = strFirst[1]
filePath = G_PATH
if '/'== fileName:
filePath += './index.html'
else:
filePath += fileName
try:
file = None
file =open(filePath,'r',encoding='gbk')
responseBody = file.read()
responseLine = 'the HTTP / 1.1 200 OK' + os.linesep
responseHeader = 'Server: ererbai' + os.linesep
responseHeader += 'Date:%s' % time.ctime() + os.linesep
except FileNotFoundError:
responseLine = 'the HTTP / 1.1 404 NOT FOUND' + os.linesep
responseHeader = 'Server: ererbai' + os.linesep
responseHeader += 'Date:%s' % time.ctime() + os.linesep
responseBody = 'Sorry, I can't find what you want on the server.'
except Exception:
responseLine = 'HTTP / 1.1 500 ERROR' + os.linesep
responseHeader = 'Server: ererbai' + os.linesep
responseHeader += 'Date: %s' % time.ctime() + os.linesep
responseBody = The server is under maintenance, please try again later. '
finally:
iffile! =None and not file.closed: file.close() senData = (responseLine + responseHeader + os.linesep + responseBody).encode('gbk')
clientSocket.send(senData)
clientSocket.close()
def main():
serveSocket=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
serveSocket.bind((' ',8000))
serveSocket.listen()
while True:
clientSocket,clientAddr = serveSocket.accept()
print(clientSocket)
multiprocessing.Process(target=serveHandler,args=(clientSocket,clientAddr)).start()
clientSocket.close()
if __name__ == '__main__':
main()
Copy the code