# 缓存页面示例
import os
from urllib.request import urlopen
# 先查看文件里是否有这个网页的内容,内部函数调用调用外部函数的变量,此时变量存在一个文件中,
# 如果没有,就把它写入文件,等第二次进行访问的时候,先进行文件判断,如果存在就直接读,
def cache(func):
def inner(*args,**kwargs):
if os.path.getsize('web_cache'):
with open('web_cache','rb') as f:
return f.read()
ret = func(*args,**kwargs)
with open('web_cache','wb') as f :
f.write(b'***'+ret)
return ret
return inner
@cache
def get(url):
code = urlopen(url).read()
return code
ret = get("https://home.cnblogs.com/u/wupeiqi/")
print(ret)
ret = get("https://home.cnblogs.com/u/wupeiqi/")
print(ret)
ret = get("https://home.cnblogs.com/u/wupeiqi/")
print(ret)
把每次添加删除的记录全部存在文件中
# def wrapper(func):
# def inner(*args,**kwargs):
#
# ret = func(*args,**kwargs)
# with open('log1','a',encoding='utf-8') as f:
# f.write(func.__name__+'
')
# return ret
#
# return inner
#
#
#
# @wrapper
# def shoplist_add():
# print('增加一个函数')
#
#
# @wrapper
# def shoplist_del():
# print('删除一个')
#
# shoplist_add()
# shoplist_del()
登陆示例,第一次成功后,后续无需每部操作都再次登陆
FLAG = False
def wrapper(func):
def inner(*args,**kwargs):
global FLAG
if FLAG:
ret = func(*args, **kwargs)
return ret
else:
username = input('username:')
password = input('passward:')
if username =='boss_gold' and password == '22222':
FLAG = True
ret = func(*args,**kwargs)
return ret
else:
print('登录失败')
return inner
@wrapper
def shoplist_add():
print('增加一个函数')
@wrapper
def shoplist_del():
print('删除一个')
# #
shoplist_add()
shoplist_del()