# The following is the development of its own web framework: # 1. # Http, stateless, short connection # 2 # Browser (socket client) # website (socket server) # 3 # to write their own website # a.socket Service end # . b routing system: # according to different URL to return different content # URL -> function # c string returned to the user. # template rendering engine (replace): # HTML serves as a template (special characters) # create their own arbitrary data # 4. # the Web frame types: # - A, B, C -> the Tornado # - [Third Party A -> the wsgiref], B, C -> the Django # - [Third Party a -> wsgiref], b, [ third party c -> jinja2] -> flask , import socket import pymysql def f1(request): Open with ( ' form.html ' , ' R & lt ' , encoding = ' UTF-. 8 ' ) AS F: # RB specify the encoding parameter is not supported during operation Content = reached, f.read () return Content def f2(request): with open('table.html','r',encoding='utf-8') as f: content = f.read() import random Age The = randomrandint (over 1,100 ), content = content.replace('@@age@@',str(age)) return content DEF F3 (Request): # 1. acquired data database Conn = pymysql.connect (Host = ' localhost ' , Port = 3306, = User ' the root ' , password = ' 123 ' , Database = ' DB1 ' , charset = ' UTF8 ' ) Cursor = conn.cursor (Cursor = pymysql.cursors.DictCursor) # result acquisition dictionary is the cursor.execute ( ' SELECT ID, username, password from UserInfo ' ) user_list = cursor.fetchall () # acquiring all the data, the dictionary is the list elements inside Print (user_list) # [{ 'ID':. 1, 'username': 'Tom', 'password': '111'}, { ' id ': 2,' username ' :' marry ',' password ':' 222 '}, {' id ': 3,' username ':' jack ',' password ':' 333 '}, {' id ' : 10, 'username': 'Alex', 'password': '555'}, { 'ID':. 11, 'username': 'Tommy', 'password': '777'}] cursor.close () conn.close() # 2. The data row on the inside to give row_list, it is a list, which elements are strings row_list = [] for dict in user_list: row = '<tr><td>%s</td><td>%s</td><td>%s</td></tr>'%(dict['id'],dict['username'],dict['password']) row_list.append(row) print(row_list) # 3 listing splicing with null character, to give a string row_str row_str = '' .join (row_list) Print (row_str) # 4. Open table1.html, read the contents, the replacement of tbody @@ row @@ into row_str, and finally returned to the browser. Open with ( ' table1.html ' , ' R & lt ' , encoding = ' UTF-. 8 ' ) AS F: content = f.read() content = content.replace('@@row@@',row_str) print(content) return content def f4(request): conn = pymysql.connect(host='localhost',port=3306,user='root',password='123',database='db1',charset='utf8') Cursor = conn.cursor (Cursor = pymysql.cursors.DictCursor) # result acquisition dictionary is the cursor.execute ( ' SELECT ID, username, password from UserInfo ' ) user_list = cursor.fetchall() cursor.close() conn.close() with open('table2.html','r',encoding='utf-8') as f: content = f.read() # Template-based rendering (replace) third-party tools to achieve from jinja2 Import Template Template = Template (Content) # the html content of the read parameter passing, to give the object instantiated Content = template.render (user_list = user_list) return Content URL_LIST = [( ' /form.html ' , F1), ( ' /table.html ' , F2), ( ' /table1.html ' , F3), ( ' /table2.html ' , F4)] # may request the url DEF RUN (): sk = socket.socket() sk.bind(('127.0.0.1',8999)) sk.listen(10) while True: conn,addr = sk.accept() Data = conn.recv (1024) .decode ( ' UTF-. 8 ' ) # reception request. If not decoded, there are many data obtained newline \ r \ n, can clearly see the structure # Print (data) header, body data.split = ( ' \ R & lt \ n-\ R & lt \ n- ' ) # request header, and responsive member is separated by two newline header_item = header.split ( ' \ R & lt \ n- ' ) # requested content with header which is a newline separated, to obtain a list # Print (header_item) gET, URL, HTTP header_item = [0] .split ( ' ' ) # first request header element is separated by a space conn.send (B ' the HTTP / 1.1 OK 200 is \ R & lt \ n-\ R & lt \ n- ' ) #http protocol # routing system: url relationship with functions, depending url, return different content for Item in URL_LIST: IF Item [0] == url: # If the url is the RET = Item [. 1] (Data) # pass the request to put the function parameters, return value to get conn.send (ret.encode ( ' UTF-. 8 ' )) BREAK the else : conn.send(b'404 not found') conn.close() if __name__ == '__main__': run()