Python uses "reflection" to implement different URLs pointing to different functions for processing (reflection application One)

Source: Internet
Author: User

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)

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.