本文整理汇总了Python中netaddr.IPNetwork.ipv4方法的典型用法代码示例。如果您正苦于以下问题:Python IPNetwork.ipv4方法的具体用法?Python IPNetwork.ipv4怎么用?Python IPNetwork.ipv4使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类netaddr.IPNetwork
的用法示例。
在下文中一共展示了IPNetwork.ipv4方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: validate_ip
# 需要导入模块: from netaddr import IPNetwork [as 别名]
# 或者: from netaddr.IPNetwork 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: test_ip_v4_to_ipv6_compatible
# 需要导入模块: from netaddr import IPNetwork [as 别名]
# 或者: from netaddr.IPNetwork import ipv4 [as 别名]
def test_ip_v4_to_ipv6_compatible():
assert IPAddress('192.0.2.15').ipv6(ipv4_compatible=True) == IPAddress('::192.0.2.15')
assert IPAddress('192.0.2.15').ipv6(ipv4_compatible=True).is_ipv4_compat()
assert IPAddress('192.0.2.15').ipv6(True) == IPAddress('::192.0.2.15')
ip = IPNetwork('192.0.2.1/23')
assert ip.ipv4() == IPNetwork('192.0.2.1/23')
assert ip.ipv6() == IPNetwork('::ffff:192.0.2.1/119')
assert ip.ipv6(ipv4_compatible=True) == IPNetwork('::192.0.2.1/119')
示例3: validate_ip
# 需要导入模块: from netaddr import IPNetwork [as 别名]
# 或者: from netaddr.IPNetwork 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
示例4: validate_ip
# 需要导入模块: from netaddr import IPNetwork [as 别名]
# 或者: from netaddr.IPNetwork 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
示例5: same_ip
# 需要导入模块: from netaddr import IPNetwork [as 别名]
# 或者: from netaddr.IPNetwork 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