>>> import ipaddress
>>> ipaddress.ip_address/'127.0.0.1'/
IPv4Address/'127.0.0.1'/
>>> ipaddress.ip_address/'277.0.0.1'/
Traceback /most recent call last/:
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.3/ipaddress.py", line 54, in ip_address
address/
ValueError: '277.0.0.1' does not appear to be an IPv4 or IPv6 address
>>> ipaddress.ip_address/'foobar'/
Traceback /most recent call last/:
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.3/ipaddress.py", line 54, in ip_address
address/
ValueError: 'foobar' does not appear to be an IPv4 or IPv6 address
def is_valid_ipv6/ip/:
"""Validates IPv6 addresses.
"""
pattern = re.compile/r"""
^
\s* # Leading whitespace
/?!.*::.*::/ # Only a single whildcard allowed
/?:/?!:/|:/?=:// # Colon iff it would be part of a wildcard
/?: # Repeat 6 times: [0-9a-f]{0,4} # A group of at most four hexadecimal digits
/?:/?<=::/|/?::/:/ # Colon unless preceeded by wildcard
/{6} #
/?: # Either [0-9a-f]{0,4} # Another group
/?:/?<=::/|/?<!::/:/ # Colon unless preceeded by wildcard [0-9a-f]{0,4} # Last group
/?: /?<=::/ # Colon iff preceeded by exacly one colon
| /?<!:/ #
| /?<=:/ /?<!::/ : #
/ # OR
| # A v4 address with NO leading zeros
/?:25[0-4]|2[0-4]\d|1\d\d|[1-9]?\d/
/?: \.
/?:25[0-4]|2[0-4]\d|1\d\d|[1-9]?\d/
/{3}
/
\s* # Trailing whitespace
$
""", re.VERBOSE | re.IGNORECASE | re.DOTALL/
return pattern.match/ip/ is not None
def is_valid_ip/ip/:
m = re.match/r"^/\d{1,3}/\./\d{1,3}/\./\d{1,3}/\./\d{1,3}/$", ip/
return bool/m/ and all/map/lambda n: 0 <= int/n/ <= 255, m.groups////
</div>
<div class="answer_text">
我想它会这样做......
def validIP/address/:
parts = address.split/"."/
if len/parts/ != 4:
return False
for item in parts:
if not 0 <= int/item/ <= 255:
return False
return True
if re.match/r'^//\d{1,2}|1\d{2}|2[0-4]\d|25[0-5]/\./{3}/\d{1,2}|1\d{2}|2[0-4]\d|25[0-5]/$', ip/:
print "Valid IP"
else:
print "Invalid IP"
</div>
<div class="answer_text">
我发明了这个简单版本
def ip_checkv4/ip/:
parts=ip.split/"."/
if len/parts/<4 or len/parts/>4:
return "invalid IP length should be 4 not greater or less than 4"
else:
while len/parts/== 4:
a=int/parts[0]/
b=int/parts[1]/
c=int/parts[2]/
d=int/parts[3]/
if a<= 0 or a == 127 :
return "invalid IP address"
elif d == 0:
return "host id should not be 0 or less than zero "
elif a>=255:
return "should not be 255 or greater than 255 or less than 0 A"
elif b>=255 or b<0:
return "should not be 255 or greater than 255 or less than 0 B"
elif c>=255 or c<0:
return "should not be 255 or greater than 255 or less than 0 C"
elif d>=255 or c<0:
return "should not be 255 or greater than 255 or less than 0 D"
else:
return "Valid IP address ", ip
p=raw_input/"Enter IP address"/
print ip_checkv4/p/
</div>
<div class="answer_text">
我只需要拆卸地址 IP v4. 我基于寒意战略的决定应该:
def validIP/address/:
parts = address.split/"."/
if len/parts/ != 4:
return False
for item in parts:
if not 0 <= int/item/ <= 255:
return False
return True
def ip_checkv4/ip/:
parts=ip.split/"."/
if len/parts/<4 or len/parts/>4:
return "invalid IP length should be 4 not greater or less than 4"
else:
while len/parts/== 4:
a=int/parts[0]/
b=int/parts[1]/
c=int/parts[2]/
d=int/parts[3]/
if a<= 0 or a == 127 :
return "invalid IP address"
elif d == 0:
return "host id should not be 0 or less than zero "
elif a>=255:
return "should not be 255 or greater than 255 or less than 0 A"
elif b>=255 or b<0:
return "should not be 255 or greater than 255 or less than 0 B"
elif c>=255 or c<0:
return "should not be 255 or greater than 255 or less than 0 C"
elif d>=255 or c<0:
return "should not be 255 or greater than 255 or less than 0 D"
else:
return "Valid IP address ", ip
p=raw_input/"Enter IP address"/
print ip_checkv4/p/
11 个回复
三叔
赞同来自:
石油百科
赞同来自:
江南孤鹜
赞同来自:
/模块旨在使用 IP 地址/ 让我们给出一个例外 ValueError 无效地址。
然而,以及尘土蒂纳的答案,他将采取这样的东西 "4" 和 "192.168", 由于已经提到,这些是有效地址 IP.
如果您正在使用 Python 3.3 或以后的版本,现在它包括
http://docs.python.org/py3k/library/ipaddress.html
:
为了 Python 2 您可以获得相同的功能 ipaddress, 如果安装 python-ipaddress:
该模块兼容 Python 2 并提供非常相似 API 在模块上 ipaddress, 包含在标准库中 Python 以。。。开始 Python 3.3. 更多细节
https://github.com/HaDiNet/pyt ... ME.md
. 在 Python 2 您需要明确转换地址字符串 IP 在 unicode:
.
</module></stdin></module></stdin>
郭文康
赞同来自:
- IPv4/IPv6 s.a.
https://docs.python.org/3/library/ipaddress.html
完整的文档。
例子 :
对于其他版本: Github, phihag / Philipp Hagemeister, "Python 3.3's 旧版本的IP地址 Python",
https://github.com/phihag/ipaddress
Budkort从 phihag 可用,例如, Anaconda Python 2.7 & 进入安装程序。 s.a.
https://docs.continuum.io/anaconda/pkg-docs
安装S. pip:
s.a.: ipaddress 1.0.17, "IPv4/IPv6 图书馆操纵", "端口模块 3.3+ ipaddress",
https://pypi.python.org/pypi/ipaddress/1.0.17
奔跑吧少年
赞同来自:
IPv4:
IPv6:
版本 IPv6 用途 "
", 可以被替换 "
" 在发动机上 regex, 支持带大面包的条件名称 /它是。 PCRE, .NET/
编辑
:
丢弃的本机选项。
扩展 regex, 匹配 RFC.
加回了 regex 为了 IPv6 地址。
Edit2
:
我发现了几个链接讨论如何拆卸地址 IPv6 从 regex:
http://forums.dartware.com/viewtopic.php?t=452
-InterMapper 论坛
http://www.vankouteren.eu/blog ... sion/
- 博客 Patrick's playground
http://download.dartware.com/t ... ex.pl
- 设想 Perl 有一堆测试。 看起来像mo. regex 落在几个测试中。
Edit3
:
最后,可以写出所有测试通过的模式,我也很高兴。
</div
<div class="answer_text">
我希望它足够简单和pyatovsky:
</div>
<div class="answer_text">
我想它会这样做......
</div>
<div class="answer_text">
我必须向Marcus Jander Dzhardot致敬 - 我的大多数帖子都受到他的帖子的启发。
我发现Marcus的答案仍然不符合一些例子 IPv6 在情景中 Perl, 他的回答是指的。
这是我的 regex, 它在此脚本中传输所有示例 Perl:
我也收集了一个剧本 Python, 检查所有这些示例 IPv6; 他在
http://pastebin.com/TKLfQUG0
, 因为他太大了在这里发表他。
您可以使用测试结果和示例的参数运行脚本 "[result]=[example]", 因为:
或者您可以在不指向任何参数的情况下启动所有测试,因此:
无论如何,我希望这将帮助别人!
</div>
<div class="answer_text">
考虑地址 IPv4 作为 "ip".
</div>
<div class="answer_text">
我发明了这个简单版本
</div>
<div class="answer_text">
我只需要拆卸地址 IP v4. 我基于寒意战略的决定应该:
def getIP//:
valid = False
while not valid :
octets = raw_input/ "Remote Machine IP Address:" /.strip//.split/"."/
try: valid=len/ filter/ lambda/item/:0<=int/item/<256, octets/ / == 4
except: valid = False
return ".".join/ octets /
</div>
石油百科
赞同来自:
三叔
赞同来自:
卫东
赞同来自:
我发现Marcus的答案仍然不符合一些例子 IPv6 在情景中 Perl, 他的回答是指的。
这是我的 regex, 它在此脚本中传输所有示例 Perl:
我也收集了一个剧本 Python, 检查所有这些示例 IPv6; 他在
http://pastebin.com/TKLfQUG0
, 因为他太大了在这里发表他。
您可以使用测试结果和示例的参数运行脚本 "[result]=[example]", 因为:
或者您可以在不指向任何参数的情况下启动所有测试,因此:
无论如何,我希望这将帮助别人!
快网
赞同来自:
君笑尘
赞同来自:
三叔
赞同来自:
def getIP//:
valid = False
while not valid :
octets = raw_input/ "Remote Machine IP Address:" /.strip//.split/"."/
try: valid=len/ filter/ lambda/item/:0<=int/item/<256, octets/ / == 4
except: valid = False
return ".".join/ octets /