比较两个IPv6地址是否相等。

huangapple go评论116阅读模式
英文:

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:

比较两个IPv6地址是否相等。

英文:

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:

比较两个IPv6地址是否相等。

答案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?

huangapple
  • 本文由 发表于 2020年1月3日 18:48:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/59577197.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定