Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
5160d51c
Commit
5160d51c
authored
Oct 21, 2013
by
Peter Moody
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
#17400; ipaddress should make it easy to identify rfc6598 addresses
parent
6815a84d
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
74 additions
and
11 deletions
+74
-11
Lib/ipaddress.py
Lib/ipaddress.py
+65
-11
Lib/test/test_ipaddress.py
Lib/test/test_ipaddress.py
+9
-0
No files found.
Lib/ipaddress.py
View file @
5160d51c
...
...
@@ -975,12 +975,24 @@ class _BaseNetwork(_IPAddressBase):
"""Test if this address is allocated for private networks.
Returns:
A boolean, True if the address is reserved per RFC 4193.
A boolean, True if the address is reserved per
iana-ipv4-special-registry or iana-ipv6-special-registry.
"""
return
(
self
.
network_address
.
is_private
and
self
.
broadcast_address
.
is_private
)
@
property
def
is_global
(
self
):
"""Test if this address is allocated for private networks.
Returns:
A boolean, True if the address is not reserved per
iana-ipv4-special-registry or iana-ipv6-special-registry.
"""
return
not
self
.
is_private
@
property
def
is_unspecified
(
self
):
"""Test if the address is unspecified.
...
...
@@ -1225,15 +1237,37 @@ class IPv4Address(_BaseV4, _BaseAddress):
"""Test if this address is allocated for private networks.
Returns:
A boolean, True if the address is reserved per RFC 1918.
A boolean, True if the address is reserved per
iana-ipv4-special-registry.
"""
private_10
=
IPv4Network
(
'10.0.0.0/8'
)
private_172
=
IPv4Network
(
'172.16.0.0/12'
)
private_192
=
IPv4Network
(
'192.168.0.0/16'
)
return
(
self
in
private_10
or
self
in
private_172
or
self
in
private_192
)
return
(
self
in
IPv4Network
(
'0.0.0.0/8'
)
or
self
in
IPv4Network
(
'10.0.0.0/8'
)
or
self
in
IPv4Network
(
'100.64.0.0/10'
)
or
self
in
IPv4Network
(
'127.0.0.0/8'
)
or
self
in
IPv4Network
(
'169.254.0.0/16'
)
or
self
in
IPv4Network
(
'172.16.0.0/12'
)
or
self
in
IPv4Network
(
'192.0.0.0/29'
)
or
self
in
IPv4Network
(
'192.0.0.170/31'
)
or
self
in
IPv4Network
(
'192.0.2.0/24'
)
or
self
in
IPv4Network
(
'192.168.0.0/16'
)
or
self
in
IPv4Network
(
'198.18.0.0/15'
)
or
self
in
IPv4Network
(
'198.51.100.0/24'
)
or
self
in
IPv4Network
(
'203.0.113.0/24'
)
or
self
in
IPv4Network
(
'240.0.0.0/4'
)
or
self
in
IPv4Network
(
'255.255.255.255/32'
))
@
property
def
is_global
(
self
):
"""Test if this address is allocated for private networks.
Returns:
A boolean, True if the address is not reserved per
iana-ipv4-special-registry.
"""
return
not
self
.
is_private
@
property
def
is_multicast
(
self
):
...
...
@@ -1826,11 +1860,31 @@ class IPv6Address(_BaseV6, _BaseAddress):
"""Test if this address is allocated for private networks.
Returns:
A boolean, True if the address is reserved per RFC 4193.
A boolean, True if the address is reserved per
iana-ipv6-special-registry.
"""
return
(
self
in
IPv6Network
(
'::1/128'
)
or
self
in
IPv6Network
(
'::/128'
)
or
self
in
IPv6Network
(
'::ffff:0:0/96'
)
or
self
in
IPv6Network
(
'100::/64'
)
or
self
in
IPv6Network
(
'2001::/23'
)
or
self
in
IPv6Network
(
'2001:2::/48'
)
or
self
in
IPv6Network
(
'2001:db8::/32'
)
or
self
in
IPv6Network
(
'2001:10::/28'
)
or
self
in
IPv6Network
(
'fc00::/7'
)
or
self
in
IPv6Network
(
'fe80::/10'
))
@
property
def
is_global
(
self
):
"""Test if this address is allocated for public networks.
Returns:
A boolean, true if the address is not reserved per
iana-ipv6-special-registry.
"""
private_network
=
IPv6Network
(
'fc00::/7'
)
return
self
in
private_network
return
not
self
.
is_private
@
property
def
is_unspecified
(
self
):
...
...
Lib/test/test_ipaddress.py
View file @
5160d51c
...
...
@@ -1319,6 +1319,11 @@ class IpaddrUnitTest(unittest.TestCase):
self
.
assertEqual
(
True
,
ipaddress
.
ip_network
(
'127.42.0.0/16'
).
is_loopback
)
self
.
assertEqual
(
False
,
ipaddress
.
ip_network
(
'128.0.0.0'
).
is_loopback
)
self
.
assertEqual
(
True
,
ipaddress
.
ip_network
(
'100.64.0.0/10'
).
is_private
)
self
.
assertEqual
(
True
,
ipaddress
.
ip_network
(
'192.0.2.128/25'
).
is_private
)
self
.
assertEqual
(
True
,
ipaddress
.
ip_network
(
'192.0.3.0/24'
).
is_global
)
# test addresses
self
.
assertEqual
(
True
,
ipaddress
.
ip_address
(
'0.0.0.0'
).
is_unspecified
)
...
...
@@ -1384,6 +1389,10 @@ class IpaddrUnitTest(unittest.TestCase):
self
.
assertEqual
(
False
,
ipaddress
.
ip_network
(
'::1'
).
is_unspecified
)
self
.
assertEqual
(
False
,
ipaddress
.
ip_network
(
'::/127'
).
is_unspecified
)
self
.
assertEqual
(
True
,
ipaddress
.
ip_network
(
'2001::1/128'
).
is_private
)
self
.
assertEqual
(
True
,
ipaddress
.
ip_network
(
'200::1/128'
).
is_global
)
# test addresses
self
.
assertEqual
(
True
,
ipaddress
.
ip_address
(
'ffff::'
).
is_multicast
)
self
.
assertEqual
(
True
,
ipaddress
.
ip_address
(
2
**
128
-
1
).
is_multicast
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment