本文整理匯總了Python中netaddr.IPAddress.ipv4方法的典型用法代碼示例。如果您正苦於以下問題:Python IPAddress.ipv4方法的具體用法?Python IPAddress.ipv4怎麽用?Python IPAddress.ipv4使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類netaddr.IPAddress
的用法示例。
在下文中一共展示了IPAddress.ipv4方法的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: validate_ip
# 需要導入模塊: from netaddr import IPAddress [as 別名]
# 或者: from netaddr.IPAddress import ipv4 [as 別名]
def validate_ip(self, request, remote_ip):
# When we aren't configured to restrict on IP address
if not getattr(settings, 'RESTRICTEDSESSIONS_RESTRICT_IP', True):
return True
# When the IP address key hasn't yet been set on the request session
if SESSION_IP_KEY not in request.session:
return True
# When there is no remote IP, check if one has been set on the session
session_ip = request.session[SESSION_IP_KEY]
if not remote_ip:
if session_ip: # session has remote IP value so validate :-(
return False
else: # Session doesn't have remote IP value so possibly :-)
return True
# Compute fuzzy IP compare based on settings on compare sensitivity
session_network = IPNetwork(session_ip)
remote_ip = IPAddress(remote_ip)
try:
session_network = session_network.ipv4()
remote_ip = remote_ip.ipv4()
session_network.prefixlen = getattr(settings, 'RESTRICTEDSESSIONS_IPV4_LENGTH', 32)
except AddrConversionError:
try:
session_network.prefixlen = getattr(settings, 'RESTRICTEDSESSIONS_IPV6_LENGTH', 64)
except AddrFormatError:
# session_network must be IPv4, but remote_ip is IPv6
return False
return remote_ip in session_network
示例2: _normalize_ip_address
# 需要導入模塊: from netaddr import IPAddress [as 別名]
# 或者: from netaddr.IPAddress import ipv4 [as 別名]
def _normalize_ip_address(addr):
"""
When we used mapped ipv4 (starting with ::FFFF/96) we need to
normalize it to ipv4 in order to compare it with value used
in commonName in the certificate.
"""
ip = IPAddress(addr)
if ip.is_ipv4_mapped():
addr = str(ip.ipv4())
return addr
示例3: normalize_mapped_address
# 需要導入模塊: from netaddr import IPAddress [as 別名]
# 或者: from netaddr.IPAddress import ipv4 [as 別名]
def normalize_mapped_address(ipaddr):
"""
Converts a IPv4-mapped IPv6 address into a IPv4 address. Handles both the
::ffff:192.0.2.128 format as well as the deprecated ::192.0.2.128 format.
:param ipaddr: IP address [str]
:return: normalized IP address [str]
"""
ipaddr = IPAddress(ipaddr)
if ipaddr.is_ipv4_compat() or ipaddr.is_ipv4_mapped():
ipaddr = ipaddr.ipv4()
return str(ipaddr)
示例4: validate_ip
# 需要導入模塊: from netaddr import IPAddress [as 別名]
# 或者: from netaddr.IPAddress import ipv4 [as 別名]
def validate_ip(self, request, remote_ip):
if not getattr(settings, 'RESTRICTEDSESSIONS_RESTRICT_IP', True) or not SESSION_IP_KEY in request.session:
return True
session_network = IPNetwork(request.session[SESSION_IP_KEY])
remote_ip = IPAddress(remote_ip)
try:
session_network = session_network.ipv4()
remote_ip = remote_ip.ipv4()
session_network.prefixlen = getattr(settings, 'RESTRICTEDSESSIONS_IPV4_LENGTH', 32)
except AddrConversionError:
session_network.prefixlen = getattr(settings, 'RESTRICTEDSESSIONS_IPV6_LENGTH', 64)
return remote_ip in session_network
示例5: validate_ip
# 需要導入模塊: from netaddr import IPAddress [as 別名]
# 或者: from netaddr.IPAddress import ipv4 [as 別名]
def validate_ip(self, request, remote_ip):
if not getattr(settings, 'RESTRICTEDSESSIONS_RESTRICT_IP', True) or not SESSION_IP_KEY in request.session:
return True
session_network = IPNetwork(request.session[SESSION_IP_KEY])
remote_ip = IPAddress(remote_ip)
try:
session_network = session_network.ipv4()
remote_ip = remote_ip.ipv4()
session_network.prefixlen = IPV4_LENGTH
except AddrConversionError:
try:
session_network.prefixlen = IPV6_LENGTH
except AddrFormatError:
# session_network must be IPv4, but remote_ip is IPv6
return False
return remote_ip in session_network
示例6: same_ip
# 需要導入模塊: from netaddr import IPAddress [as 別名]
# 或者: from netaddr.IPAddress import ipv4 [as 別名]
def same_ip(cls, orig_remote_ip, remote_ip):
# Check is disabled -- always return true
if not getattr(settings, 'RESTRICTEDSESSIONS_RESTRICT_IP', True):
return True
# No original IP or current IP is unknown
if not orig_remote_ip or not remote_ip:
return True
session_network = IPNetwork(orig_remote_ip)
remote_ip = IPAddress(remote_ip)
try:
session_network = session_network.ipv4()
remote_ip = remote_ip.ipv4()
session_network.prefixlen = getattr(settings, 'RESTRICTEDSESSIONS_IPV4_LENGTH', 32)
except AddrConversionError:
try:
session_network.prefixlen = getattr(settings, 'RESTRICTEDSESSIONS_IPV6_LENGTH', 64)
except AddrFormatError:
# session_network must be IPv4, but remote_ip is IPv6
return False
# IP belongs to the same network
return remote_ip in session_network