This article mainly introduces the Python bottle framework to solve the user authentication problem, the code based on the python2.x version, the need for friends can refer to the
The first is to analyze the requirements, Web program background needs authentication, background page contains multiple pages, the most common way is to add a certification for each URL, but this requires each of the binding URL of the background functions need to add similar or the same code, but this code is excessive redundancy, but not conducive to expansion.
Next we don't talk about adorners, we all know Python is a very powerful language, she can pass functions as arguments to the function, the simplest:
|1 2 3 4 5 6 7 8 9 10 11 12-13||Def p (): print ' Hello,world ' def funcfactor (func): print ' calling function named ', func.__name__ func () print ' End ' Fu Ncfactor (P) # output is: # calling function named P # hello,world # End|
At a glance, define a function P (), pass the function p as a parameter to shout out Funcfactor, and add some action before and after executing the P function.
We can also do this:
|1 2 3 4 5 6 7 8 9 10 11||Def p (): print ' Hello,world ' def funcfactor (func): print ' calling function named ', func.__name__ return func func = func Factor (P) func () # output is: # calling function named P Hello,world|
As you can see, we can return the function and assign it to a variable for later invocation. But in this case we want to do something after the function is executed, but our Python is powerful, python can nest a function in the function, we can do it like this:
|1 2 3 4 5 6 7 8 9 10 11 12 13 14-15 16||Def p (): print ' Hello, World ' Def Funcfactor (func): Def wrapper (): print ' does something at start ' func () print ' Do Someth ing at "return" wrapper func = Funcfactor (P) func () #输出为: # do something in start # Hello, world # doing something at en D|
Let's look at the adorner, the code above is a very difficult task to implement, but not elegant enough, and the code does not conform to the philosophy of Python, so the adorner came out, the adorner is not the same as the above principle, the same for wrapping functions, It's just that the code is more elegant and easier to read. The adorner follows the name of the adorner at the beginning of the @ and the next line is the function body to be packaged, and the example above can be implemented using the adorner:
|1 2 3 4 5 6 7 8 9 10 11 12 13 14-15 16||def decorator (func): Def wrapper (): print ' Do something in Start ' func () print ' Do something ' "return wrapper @dec Orator Def p (): print ' Hello, World ' P () #输出为: # doing something at start # Hello, world # doing something at end|
In fact, the adorner does not have the performance or other aspects of Ascension, just a syntactic sugar, is the previous example of the rewrite, so much more elegant and reading. If our P () function doesn't want to just lose hello,world, we want to greet certain people we specify:
|1 2 3 4 5 6 7 8 9 10 11 12 13 14-15 16||def decorator (func): Def wrapper (*args, **kargs): print ' Do something in Start ' func (**kargs) print ' Do something ' return wrapper @decorator def p (name): print ' Hello ', name P (name= "Jim") #输出为: # doing something at start # Hello Jim # D o Something at end|
Adorners are not required to decorate an adorner without parameters, and if the decorated function requires arguments, a function must be nested to handle the arguments. I'm sure everyone knows the usage and function of the adorner. Now back to the point, how to gracefully to the background URL with the validation function? There is no doubt that we use adorners to handle:
|1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22-23||def Blog_auth (func): ' Define an adorner to decorate the page adorner that needs to be validated must be placed under the route adorner ' ' # define wrapper function Def wrapper (*args, **kargs): Try: # Read Cookie US ER = Request. cookies[' user ' shell = Request. cookies[' Shell '] except: # An exception is redirected to the login page redirect ('/login ') # Verify user data if Checkshell (user, Shell): # Validation succeeds returns function return func (**kargs) Else: # Otherwise redirect to login page redirect ('/login ') return wrapper|
Add the Blog_auth adorner where you need to verify it again:
|1 2 3 4, 5 6 7 8 9 10 11 12 13 14 15||@route ('/admin:#/?# ') @blog_auth def admin (): "" used to display the background admin home ' template[' title '] = ' Dashboard | ' + template[' blog_name '] template[' user ' = Request. cookies[' user '] articles =  for article in Db.posts.find (). Sort ("date", descending). Limit (a): Articles.append ( Article) # Submit the list of articles to the foreground template template[' articles ' = articles return TEMPLATE (' admin.html ', TEMPLATE)|
The problem with this bottle verification is gracefully solved with the adorner.