Skip to content

Commit

Permalink
优化地址判断逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
NaNShaner committed Oct 15, 2023
1 parent 00abbd8 commit 5eef2b1
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions polls/tools.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import ipaddress

from django.core.exceptions import ValidationError
from django.utils.html import format_html
from IPy import IP
Expand All @@ -16,7 +18,8 @@ def my_custom_sql():
row_list = []
cursor = connection.cursor()
db_name = settings.DATABASES
cursor.execute(f"SELECT ip FROM {db_name['default']['NAME']}.polls_ipaddr")
# cursor.execute(f"SELECT ip FROM {db_name['default']['NAME']}.polls_ipaddr")
cursor.execute(f"SELECT ip FROM 'polls_ipaddr'")
row = cursor.fetchall()
for i in row:
row_list.append(i[0])
Expand All @@ -29,10 +32,10 @@ def judge_legal_ip(ip):
:param ip:
:return:
"""
compile_ip = re.compile('^((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)$')
if compile_ip.match(ip):
try:
ipaddress.IPv4Network(ip)
return True
else:
except ValueError:
return False


Expand Down

0 comments on commit 5eef2b1

Please sign in to comment.