本文整理汇总了Python中networkapi.ip.models.Ip.get_by_octs方法的典型用法代码示例。如果您正苦于以下问题:Python Ip.get_by_octs方法的具体用法?Python Ip.get_by_octs怎么用?Python Ip.get_by_octs使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类networkapi.ip.models.Ip
的用法示例。
在下文中一共展示了Ip.get_by_octs方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: handle_post
# 需要导入模块: from networkapi.ip.models import Ip [as 别名]
# 或者: from networkapi.ip.models.Ip import get_by_octs [as 别名]
def handle_post(self, request, user, *args, **kwargs):
"""Handles POST requests to get an IPv4 or Ipv6 by oct or blocks .
URL: ip/getbyoctblock/
"""
self.log.info("Get a Ipv4's or Ipv6's")
try:
# Business Validations
# Load XML data
xml_map, attrs_map = loads(request.raw_post_data)
# XML data format
networkapi_map = xml_map.get('networkapi')
if networkapi_map is None:
msg = u'There is no value to the networkapi tag of XML request.'
self.log.error(msg)
return self.response_error(3, msg)
ip_map = networkapi_map.get('ip_map')
if ip_map is None:
msg = u'There is no value to the ip tag of XML request.'
self.log.error(msg)
return self.response_error(3, msg)
# Get XML data
ip = ip_map.get('ip')
# User permission
if not has_perm(user, AdminPermission.IPS, AdminPermission.READ_OPERATION):
self.log.error(
u'User does not have permission to perform the operation.')
return self.not_authorized()
# Valid ip
if not is_valid_ip_ipaddr(ip):
self.log.error(u'Parameter ip is invalid. Value: %s.', ip)
raise InvalidValueError(None, 'ip', ip)
# Business Rules
version = ''
ip_list = ip.split('.')
if len(ip_list) == 1:
ip_list = ip.split(':')
ips = Ipv6.get_by_blocks(ip_list[0], ip_list[1], ip_list[2], ip_list[
3], ip_list[4], ip_list[5], ip_list[6], ip_list[7])
version = IP_VERSION.IPv6[1]
else:
ips = Ip.get_by_octs(
ip_list[0], ip_list[1], ip_list[2], ip_list[3])
version = IP_VERSION.IPv4[1]
ips_list = []
for ip in ips:
ip_dict = model_to_dict(ip)
ip_dict['version'] = version
ips_list.append(ip_dict)
return self.response(dumps_networkapi({'ips': ips_list}))
except XMLError, x:
self.log.error(u'Error reading the XML request.')
return self.response_error(3, x)