Python3 ornament and python3 Ornament

Source: Internet
Author: User

Python3 ornament and python3 Ornament

If you want to add malicious code to the program, it is a good choice to decorator!
 
 
  • The decorator function is generally defined outside the main program and is not easy to be found;
  • Second, the decorator function is executed before the main program runs. You can obtain the resources allocated by the system before the main program runs.
The syntax rules of the decorator are very interesting. You can extend it without changing the original function code to implement friendly iterative upgrade of the program. In fact, the function of the decorator is not just to change the function, if you want to, the decorator Can also "intelligently" the selection function on the background of the website. You need to classify the requests on the front-end page as follows: (1345.html, Taobao, center.html) A web page named after a letter is processed by another function. To put it bluntly, requests on different pages are classified. regular Expressions are the first choice for data classification matching! We first define two functions for processing different logics, and decorator with "Regular Expression" matching rules
# Function @ route (r "[a-z] + \ matching" character name webpage \. html ") def char (file_name, url = None): print (" webpage with 'Received name' ") print (" received file name is ", file_name) print ("matched regular url expression:", url) print ("~ "* 20) # function @ route (r" [\ d] + \ matching "number name webpage \. html ") def num (file_name, url = None): print (" webpage with 'Received number' ") print (" received file name is ", file_name) print ("matched regular url expression:", url) print ("~ "* 20)
Then define the decorator Function
# Route is used to match Function Type def route (url): # set_func is used to receive function reference def set_func (func ): # bind the regular expression to the function route_dic [url] = func print ("-- added a key-value pair to route_dic --") # call_func is used to create a new function, return the new function and replace the modified old function def call_func (file_name, url): return func (file_name, url) return call_func return set_func
The basic decorator is defined as two-layer function nesting. The outer layer is responsible for accepting reference from the original function (the function added to the decorator), the inner layer is responsible for extending the original function, and finally the outer function returns the inner function, the returned inner function replaces the original function (the function added to the decorator)
The decorator function here is a set of judgment functions on the layer of the basic decorator, so that our decorator has the "classification function"
Test code:
Import re # used to store "function reference" and "url regular expression" key-Value Pair route_dic = dict () # route is used to match Function Type def route (url ): # set_func is used to receive function references def set_func (func): # preload, bind the regular expression with the function route_dic [url] = func print ("-- added a key-value pair for route_dic --") # call_func is used to create a new function, return the new function and replace the modified old function def call_func (file_name, url): return func (file_name, url) return call_func return set_func @ route (r "[a-z] + \. html ") def char (file_name, url = None): print (" you have received Webpage ") print (" received file name ", file_name) print (" matched regular url expression: ", url) print ("~ "* 20) @ route (r" [\ d] + \. html ") def num (file_name, url = None): print (" webpage with 'Received number' ") print (" received file name is ", file_name) print ("matched regular url expression:", url) print ("~ "* 20) def match_func (file_name): for url, call_func in route_dic.items (): ret = re. match (url, file_name) if ret: print ("Page found") call_func (file_name, url) else: passdef main (): print ("route dictionary value ", route_dic) # test the match_func ("index.html") # test the match_func ("123.html") page of "number ") if _ name _ = "_ main _": main ()
Running result
 
Running result

As shown in,123.htmlAndindex.htmlAre allocated to the corresponding function execution!

Deeper: careful people will find that the decoration function is executed earlier than the main program.
  • Evidence 1: the main program is using a dictionaryroute_dicPreviously, the route function was not called,route_dicIt indicates that the route function has been called before the main function is executed;
  • Evidence 2: according to the execution results of the above program, the operation to add a key-value pair was executed twice before the main function was executed, exactly corresponding to the two decorators.
If you want to add malicious code to the program, the decorator function is a good choice!
  • The decorator function is generally defined outside the main program and is not easy to be found;
  • Second, the decorator function is executed before the main program runs. You can obtain the resources allocated by the system before the main program runs.


Author: _ Zhao _
Link: http://www.jianshu.com/p/dee05d5dfb50
Source: Simplified book
Copyright belongs to the author. For commercial reprint, please contact the author for authorization. For non-commercial reprint, please indicate the source.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.