Python检测端口IP字符串是否合法
作者:hchan 时间:2023-09-03 11:51:26
IP合法性校验是开发中非常常用的,看起来很简单的判断,作用确很大,写起来比较容易出错,今天我们来总结一下,看一下3种常用的IP地址合法性校验的方法。
不使用正则表达式的方式:
def is_ip(ip: str) -> bool:
return True if [True] * 4 == [x.isdigit() and 0 <= int(x) <= 255 for x in ip.split(".")] else False
使用正则表达式的方式
import re
def isIP(str):
p = re.compile('^((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)$')
if p.match(str):
return True
else:
return False
另一种
def checkip(hostip):
pat = re.compile(r'([0-9]{1,3})\.')
r = re.findall(pat,hostip+".")
if len(r)==4 and len([x for x in r if int(x)>=0 and int(x)<=255])==4:
return True
else:
return False
来源:https://www.cnblogs.com/hchan/p/13031788.html
标签:Python,端口,IP,字符串,合法
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
如何把Oracle 数据库从 RAC 集群迁移到单机环境
2024-01-28 06:29:05
vue使用iframe嵌入网页的示例代码
2024-05-05 09:12:04
Python matplotlib绘制图形实例(包括点,曲线,注释和箭头)
2021-08-31 22:57:06
![](https://img.aspxhome.com/file/2023/0/99110_0s.jpg)
C#访问PostGreSQL数据库的方法
2024-01-23 01:00:13
![](https://img.aspxhome.com/file/2023/5/88365_0s.jpg)
django框架使用orm实现批量更新数据的方法
2023-08-11 04:48:25
windows下mysql 5.7版本中修改编码为utf-8的方法步骤
2024-01-14 14:02:44
python实现简单日志记录库glog的使用
2023-01-07 23:14:40
使用Python构建Hopfield网络的教程
2022-12-14 01:27:52
![](https://img.aspxhome.com/file/2023/8/127728_0s.jpg)
企业生产MySQL优化介绍
2024-01-21 10:13:53
GO项目配置与使用的方法步骤
2024-05-22 17:45:57
![](https://img.aspxhome.com/file/2023/6/125096_0s.png)
解决golang 反射interface{}做零值判断的一个重大坑
2024-05-21 10:24:27
![](https://img.aspxhome.com/file/2023/8/123958_0s.png)
INSERT INTO SELECT语句与SELECT INTO FROM语句的一些区别
2024-01-19 11:21:13
Python利用Turtle绘制Technoblade的示例代码
2023-08-23 22:43:01
![](https://img.aspxhome.com/file/2023/0/62260_0s.png)
美之鉴 – 女人与Web设计
2009-12-09 15:36:00
![](https://img.aspxhome.com/file/UploadPic/200912/9/web_and_beauty-36s.jpg)
javascript实现获取图片大小及图片等比缩放的方法
2024-04-16 10:29:33
Python处理键映射值操作详解
2021-03-21 03:14:53
![](https://img.aspxhome.com/file/2023/5/105255_0s.png)
Mootools 1.2教程(6)——操纵HTML DOM元素
2008-11-20 13:19:00
python的类方法和静态方法
2021-04-14 21:05:22
JS设计模式之责任链模式实例详解
2023-03-17 08:23:42
![](https://img.aspxhome.com/file/2023/9/110559_0s.png)
flask session组件的使用示例
2022-03-17 08:13:07