flask 短信报警装饰器 在 windows 下一个 ip 一天最多发送一条短信正常,但在 Linux 服务器上可以无限发短信,因为 Linux cache 有啥限制?

2018-07-30 19:47:28 +08:00
 dapengzhao

#控制短信发送条件的装饰器 def cacheip(timeout=606024, key='ip%stype%s'): def out_func(func): def in_func(*args, **kwargs): #获取请求者 ip ip = request.remote_addr typename = request.form.get('typename') #设置 key cache_key = key%(ip,typename) print(cache_key) #首次请求 get 获取不到数据 value = cache.get(cache_key) #程序出错返回 null res = func(*args, **kwargs) #发送短信的条件程序出错并且 key 首次出现 if value is None and res is 'null': print('send message') ip = request.remote_addr typename = request.form.get('typename') data = request.form.get('data') #将 set key 值确保同一个 ip 多次请求只发一次短信,每天重置过期时间 cache.set(cache_key, ip, timeout=timeout) #发短信 send_move(ip,typename,data) return res return in_func return out_func

1003 次点击
所在节点    问与答
5 条回复
dapengzhao
2018-07-30 19:51:46 +08:00
#控制微信发送条件的装饰器
def cacheip(timeout=60*60*24, key='ip%stype%s'):
def out_func(func):
def in_func(*args, **kwargs):
#获取请求者 ip
ip = request.remote_addr
typename = request.form.get('typename')
#设置 key
cache_key = key%(ip,typename)
print(cache_key)
#首次请求 get 获取不到数据
value = cache.get(cache_key)
#程序出错返回 null
res = func(*args, **kwargs)
#发送短信的条件程序出错并且 key 首次出现
if value is None and res is 'null':
print('send message')
ip = request.remote_addr
typename = request.form.get('typename')
data = request.form.get('data')
#将 set key 值确保同一个 ip 多次请求只发一次短信,每天重置过期时间
cache.set(cache_key, ip, timeout=timeout)
#发短信
send_move(ip,typename,data)
return res
return in_func
return out_func
dapengzhao
2018-07-30 19:52:57 +08:00
格式咋调呢。。。。。。。。。。。。。。。。。。。。
#控制短信发送条件的装饰器
def cacheip(timeout=60*60*24, key='ip%stype%s'):
def out_func(func):
def in_func(*args, **kwargs):
#获取请求者 ip
ip = request.remote_addr
typename = request.form.get('typename')
#设置 key
cache_key = key%(ip,typename)
print(cache_key)
#首次请求 get 获取不到数据
value = cache.get(cache_key)
#程序出错返回 null
res = func(*args, **kwargs)
#发送短信的条件程序出错并且 key 首次出现
if value is None and res is 'null':
print('send message')
ip = request.remote_addr
typename = request.form.get('typename')
data = request.form.get('data')
#将 set key 值确保同一个 ip 多次请求只发一次短信,每天重置过期时间
cache.set(cache_key, ip, timeout=timeout)
#发短信
send_move(ip,typename,data)
return res
return in_func
return out_func
ysc3839
2018-07-30 20:41:21 +08:00
你代码的格式都丢了,建议你把代码贴到第三方网站上,比如 https://paste.ubuntu.com
dapengzhao
2018-07-31 09:31:12 +08:00
dapengzhao
2018-07-31 09:31:37 +08:00
@ysc3839 好的谢谢已经贴了

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/475459

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX