import re import pymysql URL_DICT = dict() ''' Add to the regular routing reasons: in the actual development, URL will often with a lot of parameters, such as: /add/0000007.html, which 000007 (stock code means: extracts the corresponding database may be used for recording) is parameter, If this time is not regular, then we should write N times @route, add the corresponding function to the dictionary, then the dictionary of key-value pairs with N, a waste of space, If regular, then only need to write one @route to complete multiple URL, such as /add/00007.html,add/000036.html correspond to the same function, when the dictionary key-value pairs A lot less ''' def route(url): def set_func(func): URL_DICT[url] = func def call_func(): func() return call_func return set_func @route(r'/center.html') def center_p(set): pass @route (R & lt ' /add/(\d+)\.html ' ) DEF add_focus (RET): # Get ticker stock_code ret.group = (. 1 ) # determines whether a stock ticker Conn = pymysql.connect ( ' localhost ' , ' the root ' , ' ' , ' python_test ' ) cursor = conn.cursor() sql = "select * from info where code=%s" cursor.execute(sql, (stock_code,)) if not cursor.fetchone(): cursor.close() conn.close() return ' did not run the stock, we are start-up companies, please popular men ' the else : # determine whether the stock code is already concerned about SQL = ' the SELECT * Focus from the WHERE info_id =% S ' cursor.execute(sql, (stock_code,)) if cursor.fetchone(): cursor.close() conn.close() return ' has been a concern, do not repeat concerned ' the else : # Add concern SQL = ' INSERT INTO Focus (info_id) the SELECT the above mentioned id info from the WHERE code =% S ' cursor.execute(sql, (stock_code,)) conn.commit() cursor.close() conn.close() return 'add(%s) ok ....' % stock_code @route (R & lt ' /add/(\d+)\.html ' ) DEF del_focus (RET): # Get ticker stock_code ret.group = (. 1 ) # determines whether a stock ticker Conn = pymysql.connect ( ' localhost ' , ' the root ' , ' ' , ' python_test ' ) cursor = conn.cursor() sql = "select * from info as i inner join focus as f on i.id=f.info_id where i.code=%s" cursor.execute(sql, (stock_code,)) if not cursor.fetchone(): cursor.close() conn.close() return ' did not run the stock, we are start-up companies, please popular men ' the else : # determine whether the stock code is already concerned about SQL = ' the SELECT * Focus from the WHERE info_id =% S ' cursor.execute(sql, (stock_code,)) if not cursor.fetchone(): cursor.close() conn.close() return ' not previously concerned, can not be canceled ' # added concern SQL = ' the Delete INTO Focus (info_id) the SELECT the above mentioned id info from the WHERE code =% S ' cursor.execute(sql, (stock_code,)) conn.commit() cursor.close() conn.close() return 'add(%s) ok ....' % stock_code @route(r'index.html') def index_p(ret): pass DEF file application (the env, The start_response): '' ' the env is an empty dictionary, The start_response is quoted web server in a method, a function return the body ' '' The start_response ( ' 200 is the OK ' , [( ' the Content-the Type ' , ' text / HTML; charset = UTF-. 8 ' )]) file_name = env['PATH INFO'] try: # return URL_DICT[file_name]() for url, func in URL_DICT.items(): ''' {r"/index.html":index, r"/center.html":center, r"/add/\d+\.html":add_focus } '' ' RET = re.match (URL, file_name) IF RET: return FUNC (RET) the except Exception AS RET: return ' an abnormality S% ' % STR (RET)