程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python web framework improves web page routing

編輯:Python

Purpose : For support WSGI Add routing function to the web page framework of

Basic framework :

import time
def login():
return ' This is the landing page , current time :{}'.format(time.ctime())
def index():
return ' This is the home page , current time :{}'.format(time.ctime())
def application(env,start_response):
# Call the passed method , Return the response status code and header information
start_response('200 OK',[('Content-Type','text/html;charset=UTF-8')])
file_name = env['path_info']
if file_name == 'login.py':
return login()
elif file_name == 'index.py':
return index()
else:
return 'hello world! Winter came body'

among fille_name The address after the port accessed by the browser, for example  http://127.0.0.1:8080/login.py  Medium login.py

First step : Get rid of if sentence

Ideas : take file_name And function references in the dictionary , Access through a dictionary

import time
def login():
return ' This is the landing page , current time :{}'.format(time.ctime())
def index():
return ' This is the home page , current time :{}'.format(time.ctime())
URL_FUNC_DICT ={
'login.py':login ,
'index.py':index
}
def application(env,start_response):
# Call the passed method , Return the response status code and header information
start_response('200 OK',[('Content-Type','text/html;charset=UTF-8')])
file_name = env['path_info']
func = URL_FUNC_DICT[file_name]
return func()

The second step : Get rid of the manual dictionary , Let the program write a dictionary automatically

Ideas : Through ornaments , Complete the addition of the dictionary

import time
URL_FUNC_DICT =dict()
def route(url):
def set_func(func):
URL_FUNC_DICT[url] = func
def aaa(*args,**kwargs):
return func(*args,**kwargs)
return aaa
return set_func
@route('login.py')
def login():
return ' This is the landing page , current time :{}'.format(time.ctime())
@route('index.py')
def index():
return ' This is the home page , current time :{}'.format(time.ctime())
def application(env,start_response):
# Call the passed method , Return the response status code and header information
start_response('200 OK',[('Content-Type','text/html;charset=UTF-8')])
file_name = env['path_info']
func =URL_FUNC_DICT[file_name]
return func()

The third step : Supporting image      /add/151.py  perhaps https://blog.csdn.net/m0_55415810  In this form url Address .

Ideas : Use regular expressions to match url , Then return the fixed function

import re
import time
def login():
return ' This is the landing page , current time :{}'.format(time.ctime())
def index():
return ' This is the home page , current time :{}'.format(time.ctime())
def add_founc():
return 'add OK'
URL_FUNC_DICT = {
r'login.py': login,
r'index.py': index,
r'add/\d+.py':add_founc
}
def application(env, start_response):
# Call the passed method , Return the response status code and header information
start_response('200 OK', [('Content-Type', 'text/html;charset=UTF-8')])
file_name = env['path_info']
print('file_name'+':'+file_name)
for url ,func in URL_FUNC_DICT.items():
ret= re.match(url,file_name)
if ret:
return func()

Recycle  URL_FUNC_DICT  Key value pairs in the dictionary , As re.match Parameters of , If the match is successful , Then the regular expression that matches successfully is returned as key, Take the corresponding value ( Corresponding function ), And then call

That is, the fixed key value pairs in the dictionary , Become an unfixed key Corresponding Fixed value .

Then put the decorator in

import time,re
URL_FUNC_DICT =dict()
def route(url):
def set_func(func):
URL_FUNC_DICT[url] = func
def aaa(*args,**kwargs):
return func(*args,**kwargs)
return aaa
return set_func
@route(r'login.py')
def login():
return ' This is the landing page , current time :{}'.format(time.ctime())
@route(r'index.py')
def index():
return ' This is the home page , current time :{}'.format(time.ctime())
@route(r'add/\d+.py')
def add_func():
return 'add OK'
def application(env, start_response):
# Call the passed method , Return the response status code and header information
start_response('200 OK', [('Content-Type', 'text/html;charset=UTF-8')])
file_name = env['path_info']
print('file_name'+':'+file_name)
for url ,func in URL_FUNC_DICT.items():
ret= re.match(url,file_name)
if ret:
return func()

The above only supports this format , But the function visited , Is constant , That is, no parameters are passed , Now I want to pass parameters to the function

Ideas : Using regular group, Generate specific results that match

import time,re
URL_FUNC_DICT =dict()
def route(url):
def set_func(func):
URL_FUNC_DICT[url] = func
def aaa(*args,**kwargs):
return func(*args,**kwargs)
return aaa
return set_func
@route(r'login.py')
def login(ret):
return ' This is the landing page , current time :{}'.format(time.ctime())
@route(r'index.py')
def index(ret):
return ' This is the home page , current time :{}'.format(time.ctime())
@route(r'add/(\d*).py')
def add_func(ret):
code=ret.group(1)
if code:
print(' Matching results :%s'%code)
return 'add OK Matching results :%s'%code
def application(env, start_response):
# Call the passed method , Return the response status code and header information
start_response('200 OK', [('Content-Type', 'text/html;charset=UTF-8')])
file_name = env['path_info']
print('file_name'+':'+file_name)
for url ,func in URL_FUNC_DICT.items():
ret= re.match(url,file_name)
if ret:
return func(ret)

stay application in , Regular matching to the result , Isn't empty , Call the corresponding function , And will re.match Regular expressions are passed to functions , Function if you want to use , be group Generate , no need , It doesn't matter


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved