Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitf22bf8e

Browse files
opavlyukblurb-it[bot]asvetlov
authored
gh-87799: Improve the textual representation of IPv4-mapped IPv6 addresses (#29345)
Represent IPv4-mapped IPv6 address as x:x:x:x:x:x:d.d.d.d,where the 'x's are the hexadecimal valuesof the six high-order 16-bit pieces of the address,and the 'd's are the decimal valuesof the four low-order 8-bit pieces of the address(standard IPv4 representation).---------Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>Co-authored-by: Andrew Svetlov <andrew.svetlov@gmail.com>
1 parent520efec commitf22bf8e

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
lines changed

‎Lib/ipaddress.py‎

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1923,8 +1923,40 @@ def __init__(self, address):
19231923

19241924
self._ip=self._ip_int_from_string(addr_str)
19251925

1926+
def_explode_shorthand_ip_string(self):
1927+
ipv4_mapped=self.ipv4_mapped
1928+
ifipv4_mappedisNone:
1929+
long_form=super()._explode_shorthand_ip_string()
1930+
else:
1931+
prefix_len=30
1932+
raw_exploded_str=super()._explode_shorthand_ip_string()
1933+
long_form="%s%s"% (raw_exploded_str[:prefix_len],str(ipv4_mapped))
1934+
returnlong_form
1935+
1936+
def_ipv4_mapped_ipv6_to_str(self):
1937+
"""Return convenient text representation of IPv4-mapped IPv6 address
1938+
1939+
See RFC 4291 2.5.5.2, 2.2 p.3 for details.
1940+
1941+
Returns:
1942+
A string, 'x:x:x:x:x:x:d.d.d.d', where the 'x's are the hexadecimal values of
1943+
the six high-order 16-bit pieces of the address, and the 'd's are
1944+
the decimal values of the four low-order 8-bit pieces of the
1945+
address (standard IPv4 representation) as defined in RFC 4291 2.2 p.3.
1946+
1947+
"""
1948+
ipv4_mapped=self.ipv4_mapped
1949+
ifipv4_mappedisNone:
1950+
raiseAddressValueError("Can not apply to non-IPv4-mapped IPv6 address %s"%str(self))
1951+
high_order_bits=self._ip>>32
1952+
return"%s:%s"% (self._string_from_ip_int(high_order_bits),str(ipv4_mapped))
1953+
19261954
def__str__(self):
1927-
ip_str=super().__str__()
1955+
ipv4_mapped=self.ipv4_mapped
1956+
ifipv4_mappedisNone:
1957+
ip_str=super().__str__()
1958+
else:
1959+
ip_str=self._ipv4_mapped_ipv6_to_str()
19281960
returnip_str+'%'+self._scope_idifself._scope_idelseip_str
19291961

19301962
def__hash__(self):

‎Lib/test/test_ipaddress.py‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1321,6 +1321,17 @@ def testGetIp(self):
13211321
self.assertEqual(str(self.ipv6_scoped_interface.ip),
13221322
'2001:658:22a:cafe:200::1')
13231323

1324+
deftestIPv6IPv4MappedStringRepresentation(self):
1325+
long_prefix='0000:0000:0000:0000:0000:ffff:'
1326+
short_prefix='::ffff:'
1327+
ipv4='1.2.3.4'
1328+
ipv6_ipv4_str=short_prefix+ipv4
1329+
ipv6_ipv4_addr=ipaddress.IPv6Address(ipv6_ipv4_str)
1330+
ipv6_ipv4_iface=ipaddress.IPv6Interface(ipv6_ipv4_str)
1331+
self.assertEqual(str(ipv6_ipv4_addr),ipv6_ipv4_str)
1332+
self.assertEqual(ipv6_ipv4_addr.exploded,long_prefix+ipv4)
1333+
self.assertEqual(str(ipv6_ipv4_iface.ip),ipv6_ipv4_str)
1334+
13241335
deftestGetScopeId(self):
13251336
self.assertEqual(self.ipv6_address.scope_id,
13261337
None)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Improve the textual representation of IPv4-mapped IPv6 addresses (:rfc:`4291` Sections 2.2, 2.5.5.2) in:mod:`ipaddress`. Patch by Oleksandr Pavliuk.

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp