Python----------Decorator app Exercise

Source: Internet
Author: User

1. Write adorners, add authentication functions to multiple functions (the user's account password is from the file),
Requires login success once, subsequent functions do not need to enter the user name and password
Note: Reading a dictionary from a file in string form can be used with eval (' {' name ': ' Egon ', ' Password ': ' 123 '} ')
Turn into a dictionary format
1 define a dictionary to see User login status2auth_status={3     'User': None,4     'Status': False#status is not logged in5 }6 defWrapper (fun):7     defInner (*args,**Kwargs):8         ifauth_status['Status']:#If you are logged in, execute the fun function9ret = Fun (*args, **kwargs)#Index/homeTen             returnret One         Else:#If you are not logged in, you can implement the authentication function AUsername = input ('name:>>'). Strip ()#Get user name -Password = input ('password:>>'). Strip ()#Get Password -F=open ('Login.txt','R', encoding='Utf-8')#open a file to get the user's information theUser_dic=f.read ()#the type of string is read . -Zhddict=eval (User_dic)#Convert to Dictionary type -             #print (Type (zhddict)) #查看eval转换后的类型 -             ifZhddict.get (username) andPassword = = Zhddict[username]:#if the user name and password are correct, the login success is displayed, and the logon failure is displayed incorrectly +                 Print('Login Successful') -auth_status['User']=username#after successful login, put the username in user +auth_status['Status']=true if the state changes to True Aret = Fun (*args,**kwargs)#Index/home at                 returnret -             Else: -                 Print('Login Faild') -     returnInner -@wrapper#Grammar Sugar - defindex (): in     Print("Welcome to Homepage") -  to @wrapper + defHome (): -     Print("Welcome Home .") the  * index () $ index ()Panax Notoginseng Home () -Index ()
View Code

2. Write a function to download the content of the Web page.

The requirement function is: The user passes in a URL, the function returns the result of the download page

Write adorners to implement the ability to cache Web page content:

Specific: The implementation of the download page is stored in the file, if there is a value in the file (the file size is not 0), it is preferred to read the page content from the file,

Otherwise, download it and save it to a file

1  fromUrllib.requestImportUrlopen2Url_l=[]#Store3 defHuancun (func):4     " "5 implementing the ability to cache Web page content6     " "7     defInner (*args,**kwargs):#args Pass the URL, then get the URL to use the URL = args[0]8url = args[0]#Fetch URL9Filename=str (hash (URL))#use the hash algorithm to convert the URL to a number as a file name, but the number cannot be used as the file name and must be converted to STR typeTen         ifUrlinchurl_l:#If the URL is already in the list, it means you've already downloaded it, so you don't have to do the func, just open it. OneF=open (filename,'RB') Aret=F.read () -         Else:#Add to URL list if not listed - url_l.append (URL) theret = func (*args, * *Kwargs) -F=open (filename,'WB')#Open File -F.write (ret)#and write it down in a file . - f.close () +         returnret -     returnInner +  A  at@huancun#Get=huancun (GET) - defGet (URL):#define a Get function -     returnUrlopen (URL). Read ()#returns the page read - Print(Get ('http://www.people.com.cn')) - Print(Get ('http://www.people.com.cn'))#call the Get method and pass a ' http://www.people.com.cn ' URL in - Print(Get ('http://www.people.com.cn'))
View Code

Python----------Decorator app Exercise

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.