1. Brief summary
The following defines three different modules for testing, with login () and logout () in the account.py module, and index () in the admin.py module. In this case, the access to a URL is simulated by accessing a different URL to point to a function in a different module, or to handle the URL Request. So this is done through a "reflection" mechanism.
To expand, now almost all of the languages (whether Php,python,java,.net) Web frameworks are based on a "reflection" mechanism that points to different methods within different functions depending on the url.
2. account.py Module
#-*-coding:utf-8-*-"" "Created on Sun Nov 23:19:03 2016@author:toby" "" def login (): print ' login ' def logout () : print ' logout '
3. admin.py Module
#-*-coding:utf-8-*-"" "Created on Sun Nov 23:19:23 2016@author:toby" "" def index (): print ' Welcome login admin Backend '
4. index-test.py Module
#-*-coding:utf-8-*-"" "Created on Sun Nov 23:20:27 2016@author:toby" "" data = raw_input (' Please enter address: ') array = data.split ( '/') #url规则, divided by "/", the value of index 0 is the module, the value of index 1 is the function name userspance = __import__ (array[0]) #这里导入模块, The module is located in the split index 0 position func = getattr (u Serspance,array[1]) #这里通过getattr来实现反射, The position of index 1 is function func () #执行反射后的函数
5, The following is the test results
[email protected]:~/workspace/pydev/main$ python index-test.py
Please enter Address: Account/login
Login
[email protected]:~/workspace/pydev/main$ python index-test.py
Please enter Address: admin/index #admin是模块, index is a function in the module (here to simulate a url, can also be imagined as Http://192.168.1.100/admin/index)
Welcome to log in to the admin background #这个输出就是index () an action in the function
[email protected]:~/workspace/pydev/main$
This article is from the "fa&it-q group: 223843163" blog, Please be sure to keep this source http://freshair.blog.51cto.com/8272891/1872650
Python uses "reflection" to implement different URLs pointing to different functions for processing (reflection application One)