forked from kjhuanhao/chatgpt-magic-plug
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_ip.py
64 lines (55 loc) · 1.82 KB
/
check_ip.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# -*- coding: utf-8 -*-
# @Author : LaiJiahao
# @Time : 2022/12/13 17:41
# @File : check_ip.py
# @Project : 未命名
# @desc :
from AiRedis import RedisConfig
import time
MyRedis = RedisConfig()
r = MyRedis.UseRedis()
def check_ip_s(user_ip):
nowTime = str(time.time()).split('.')[0]
nowTime = int(round(int(nowTime)))
if r.get(str(user_ip)):
r.hset('BLACK_IP', user_ip, str(nowTime))
if r.hget('BLACK_IP', user_ip) :
state = {
'code': 403,
"msg" : "由于请求过于频繁,你的IP已禁,请联系开发者解除"
}
return state
else:
if r.hget('IP_INFO', user_ip):
#先判断有没有10个
redis_time = eval(r.hget('IP_INFO', user_ip))
count = nowTime - redis_time[0]
if len(redis_time) < 10:
redis_time.append(nowTime)
r.hset('IP_INFO', user_ip, str(redis_time))
state = {
'code': 200
}
return state
elif count < 60 and len(redis_time) == 10:
r.set(str(user_ip),'wait',ex=30)
state = {
"code": 202,
"msg": "请勿频繁搜索,请30S后再进行搜索,否则将禁止使用此脚本"
}
return state
elif count > 60 and len(redis_time) == 10:
pre_list = [nowTime]
r.hset('IP_INFO', user_ip, str(pre_list))
state = {
'code': 200
}
return state
else:
pre_list = [nowTime]
r.hsetnx('IP_INFO', user_ip, str(pre_list))
state = {
'code': 200
}
return state
# print(check_ip_s('127.0.0.1'))