英文:
Compare two IPv6 addresses for equality
问题
我需要比较两个IPv6地址是否相等。
这不是一个简单的字符串比较,因为相同的地址可以以多种方式编写。
我不能使用第三方包。
例如:
2041:0000:140F:0000:0000:0000:875B:131B
2041:0000:140F::875B:131B
2041:0:140F::875B:131B
而从Windows IP配置中可能是:2041:0:140F::875B:131B%11
英文:
I need to compare two IP version6, if there are equal.
It is not a simple string comparison because the same address can be written in multiple ways.
I can't use third-party packages.
For example:
2041:0000:140F:0000:0000:0000:875B:131B
2041:0000:140F::875B:131B
2041:0:140F::875B:131B
and from windows ip config can be: 2041:0:140F::875B:131B%11
答案1
得分: 3
如果您使用Python 3.3+,您可以使用标准库模块 ipaddress
:
Python 3.7.5 (default, Dec 15 2019, 17:54:26)
[GCC 9.2.1 20190827 (Red Hat 9.2.1-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import ipaddress
>>> a=ipaddress.ip_address('2041:0000:140F:0000:0000:0000:875B:131B')
>>> b=ipaddress.ip_address('2041:0000:140F::875B:131B')
>>> c=ipaddress.ip_address('2041:0:140F::875B:131B')
>>> a==b==c
True
英文:
If you are on Python 3.3+ you can use the standard library module ipaddress
:
Python 3.7.5 (default, Dec 15 2019, 17:54:26)
[GCC 9.2.1 20190827 (Red Hat 9.2.1-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import ipaddress
>>> a=ipaddress.ip_address('2041:0000:140F:0000:0000:0000:875B:131B')
>>> b=ipaddress.ip_address('2041:0000:140F::875B:131B')
>>> c=ipaddress.ip_address('2041:0:140F::875B:131B')
>>> a==b==c
True
答案2
得分: 2
你可以尝试使用 socket
库:
import socket
ip1 = "2041:0000:140F:0000:0000:0000:875B:131B"
ip2 = "2041:0000:140F::875B:131B"
ip3 = "2041:0000:140F::875B:131B"
if socket.inet_pton(socket.AF_INET6, ip1) == socket.inet_pton(socket.AF_INET6, ip2) == socket.inet_pton(socket.AF_INET6, ip3):
print ("match")
Output:
英文:
you can try with socket
library :
import socket
ip1 = "2041:0000:140F:0000:0000:0000:875B:131B"
ip2 = "2041:0000:140F::875B:131B"
ip3 = "2041:0000:140F::875B:131B"
if socket.inet_pton(socket.AF_INET6, ip1) == socket.inet_pton(socket.AF_INET6, ip2) == socket.inet_pton(socket.AF_INET6, ip3):
print ("match")
Output:
答案3
得分: -1
以下是翻译好的代码部分:
import sys
def extend_ipv6(src):
parts = src.split(":")
if '%' in parts[-1]:
parts[-1], _ = parts[-1].split('%', 2)
n_parts = len(parts)
out = list()
for part in parts:
if len(part) == 0:
for i in range(8-n_parts+1):
out.append("0000")
else:
out.append("0"*(4-len(part))+part)
return ":".join(out)
def main():
for arg in sys.argv[1:]:
print(extend_ipv6(arg))
if __name__ == "__main__":
main()
python3 do.py 2041:0000:140F:0000:0000:0000:875B:131B 2041:0000:140F::875B:131B 2041:0:140F::875B:131B 2041:0:140F::875B:131B%11
2041:0000:140F:0000:0000:0000:875B:131B
2041:0000:140F:0000:0000:0000:875B:131B
2041:0000:140F:0000:0000:0000:875B:131B
2041:0000:140F:0000:0000:0000:875B:131B
希望这对你的任务足够了。
英文:
For example:
import sys
def extend_ipv6(src):
parts = src.split(":")
if '%' in parts[-1]:
parts[-1], _ = parts[-1].split('%', 2)
n_parts = len(parts)
out = list()
for part in parts:
if len(part) == 0:
for i in range(8-n_parts+1):
out.append("0000")
else:
out.append("0"*(4-len(part))+part)
return ":".join(out)
def main():
for arg in sys.argv[1:]:
print(extend_ipv6(arg))
if __name__ == "__main__":
main()
python3 do.py 2041:0000:140F:0000:0000:0000:875B:131B 2041:0000:140F::875B:131B 2041:0:140F::875B:131B 2041:0:140F::875B:131B%11
2041:0000:140F:0000:0000:0000:875B:131B
2041:0000:140F:0000:0000:0000:875B:131B
2041:0000:140F:0000:0000:0000:875B:131B
2041:0000:140F:0000:0000:0000:875B:131B
will be this solution enough for your task?
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论