Import Re Import pymysql '' ' blog Park CSDN 51CTO Open Source China gitbub know almost simple book ' '' URL_DICT = dict () '' ' to the routing added reason regular: When the actual development, URL in often with a lot of parameters, for example: /add/0000007.html, wherein 000007 (stock code means: extracts the corresponding database may be used for recording) is the parameter, if this time is not regular, then we should write N times @route, the corresponding function is added to the dictionary in which case the key to the dictionary there are N, waste of space, if regular, then only need to write one @route to complete multiple URL, such as correspondence /add/00007.html,add/000036.html the same function, when the key-value dictionary of 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 & lt ' /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 = "* the WHERE code info from the SELECT =% S " cursor.execute (SQL, (stock_code,)) IF not cursor.fetchone (): cursor.close () conn.Close () return ' does not run the stock, we are entrepreneurial the company, 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 (): the Cursor. use Close () conn.Close () return ' has been a concern, do not repeat concerned ' the else : # Add Follow localhost = SQL ' INSERT INTO Focus (info_id) SELECT from ID code =% S WHERE info ' the cursor.execute (SQL, (stock_code,)) conn.commit () cursor.close () conn.Close () return ' the Add (% S) .... OK ' % stock_code @route (R & lt ' /add/(\d+)\.html ' ) DEF del_focus (RET): # Get ticker stock_code ret.group = (. 1 ) # determines whether the ticker Conn = pymysql.connect ( ' ' , ' the root ' , ' ' , ' python_test ' ) Cursor = conn.cursor () SQL = " SELECT * info from the Join Inner Focus AS AS I 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 ' does not run the stock, we are start-up companies, please popular men ' the else : # judge whether the stock code is already concerned about SQL = ' the SELECT * focus from the WHERE info_id =% S ' the cursor.execute (SQL, (stock_code,)) IF Not cursor.fetchone (): cursor.close() conn.close() return '之前未关注,无法取消' # 添加关注 sql = 'delete into focus (info_id) select id from info 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 @route(r'/update/(\d+)/(.*)\.html') defshow_update_age (RET): # Get ticker stock_code ret.group = (. 1 ) show_info = ret.group (2 ) Conn = pymysql.connect ( ' localhost ' , ' the root ' , '' , ' python_test ' ) Cursor = Conn. Cursor () SQL = '' '' '' cursor.execute(sql, (show_info, stock_code)) conn.commit() cursor.close() conn.Close () DEF file application (the env, The start_response): '' ' the env is an empty dictionary, is a reference to a web server in The start_response a method, a function return the body ' '' The start_response ( ' 200 is the OK ' , [( ' the Type-the Content ' , ' text / HTML; charset = UTF-. 8 ' )]) file_name = the env [ ' the PATH the INFO ' ] the try : # return URL_DICT [file_name] () for URL, FUNC in URL_DICT.items (): ' '' {R & lt "/index.html": index, r"/center.html":center, r"/add/\d+\.html":add_focus } ''' ret = re.match(url, file_name) if ret: return func(ret) except Exception as ret: return ' an abnormality S% ' % STR (RET)