本文整理汇总了Python中netaddr.IPAddress.format方法的典型用法代码示例。如果您正苦于以下问题:Python IPAddress.format方法的具体用法?Python IPAddress.format怎么用?Python IPAddress.format使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类netaddr.IPAddress
的用法示例。
在下文中一共展示了IPAddress.format方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: reload
# 需要导入模块: from netaddr import IPAddress [as 别名]
# 或者: from netaddr.IPAddress import format [as 别名]
def reload(self):
self._nameservers = []
ns = self._service.nameserversConfig()
for n in ns:
ip = IPAddress(n)
if ip.version == 4:
cfg = ConfigIP( default=toIP4List(ip.format()))
self._nameservers.append(cfg)
elif ip.version == 6:
cfg = ConfigIP6(default=ip.format())
self._nameservers.append(cfg)
示例2: test_ipaddress_v6
# 需要导入模块: from netaddr import IPAddress [as 别名]
# 或者: from netaddr.IPAddress import format [as 别名]
def test_ipaddress_v6():
ip = IPAddress('fe80::dead:beef')
assert ip.version == 6
assert repr(ip) == "IPAddress('fe80::dead:beef')"
assert str(ip) == 'fe80::dead:beef'
assert ip.format() == 'fe80::dead:beef'
assert int(ip) == 338288524927261089654018896845083623151
assert hex(ip) == '0xfe8000000000000000000000deadbeef'
assert ip.bin == '0b11111110100000000000000000000000000000000000000000000000000000000000000000000000000000000000000011011110101011011011111011101111'
assert ip.bits() == '1111111010000000:0000000000000000:0000000000000000:0000000000000000:0000000000000000:0000000000000000:1101111010101101:1011111011101111'
assert ip.words == (65152, 0, 0, 0, 0, 0, 57005, 48879)
示例3: get_version
# 需要导入模块: from netaddr import IPAddress [as 别名]
# 或者: from netaddr.IPAddress import format [as 别名]
def get_version(request):
func = request.GET.get('func', '')
remote_addr = IPAddress(request.remote_addr)
data = {
'address': remote_addr.format(),
'version': remote_addr.version,
'ipv4_mapped': remote_addr.is_ipv4_mapped(),
}
return Response(
body='%s(%s);' % (func, json.dumps(data)),
content_type='text/javascript')
示例4: test_ipaddress_v4
# 需要导入模块: from netaddr import IPAddress [as 别名]
# 或者: from netaddr.IPAddress import format [as 别名]
def test_ipaddress_v4():
ip = IPAddress('192.0.2.1')
assert ip.version == 4
assert repr(ip) == "IPAddress('192.0.2.1')"
assert str(ip) == '192.0.2.1'
assert ip.format() == '192.0.2.1'
assert int(ip) == 3221225985
assert hex(ip) == '0xc0000201'
assert ip.bin == '0b11000000000000000000001000000001'
assert ip.bits() == '11000000.00000000.00000010.00000001'
assert ip.words == (192, 0, 2, 1)
示例5: IpAddress
# 需要导入模块: from netaddr import IPAddress [as 别名]
# 或者: from netaddr.IPAddress import format [as 别名]
class IpAddress(DatabaseObject):
''' Wraps the netaddr IPAddress class '''
uuid = Column(String(36), unique=True, nullable=False, default=lambda: str(uuid4()))
box_id = Column(Integer, ForeignKey('box.id'), nullable=False)
_address = Column(String(40), unique=True)
_ip_address = None
@classmethod
def all(cls):
''' Returns a list of all objects in the database '''
return dbsession.query(cls).all()
@classmethod
def by_id(cls, _id):
''' Returns a the object with id of _id '''
return dbsession.query(cls).filter_by(id=_id).first()
@classmethod
def by_uuid(cls, _uuid):
''' Return and object based on a _uuid '''
return dbsession.query(cls).filter_by(uuid=_uuid).first()
@classmethod
def by_address(cls, addr):
''' Return and object based on an address '''
return dbsession.query(cls).filter_by(address=addr).first()
@property
def address(self):
if self._ip_address is None:
self._ip_address = IPAddress(self._address)
return self._ip_address.format()
@address.setter
def address(self, value):
ip = IPAddress(value)
if ip.is_loopback():
raise ValueError("You cannot use a loopback address")
if ip.is_multicast():
raise ValueError("You cannot use a multicast address")
self._address = value
@property
def version(self):
if self._ip_address is None:
self._ip_address = IPAddress(self._address)
return self._ip_address.version
@property
def is_private(self):
if self._ip_address is None:
self._ip_address = IPAddress(self._address)
return self._ip_address.is_private()
def to_xml(self, parent):
ip_elem = ET.SubElement(parent, "ip")
ip_elem.set("version", str(self.version))
ET.SubElement(ip_elem, "address").text = self.address
def __repr__(self):
return "<IpAddress - %s>" % self.address
def __str__(self):
return self._address
def __eq__(self, other):
return self._address == other._address
def __ne__(self, other):
return not self == other
示例6: formatIp
# 需要导入模块: from netaddr import IPAddress [as 别名]
# 或者: from netaddr.IPAddress import format [as 别名]
def formatIp(ipAddress):
ip = IPAddress(ipAddress)
if ip.version == 6:
return '[%s]' % ip.format()
return ip.format()
示例7: IPAddress
# 需要导入模块: from netaddr import IPAddress [as 别名]
# 或者: from netaddr.IPAddress import format [as 别名]
#!/usr/bin/env python
from netaddr import IPAddress, IPNetwork
import pprint
#single address
ip = IPAddress("192.168.1.31")
print "ip.format(): {}".format(ip.format())
print "str(ip): {}".format(str(ip))
#netblocks
netblock = IPNetwork("192.168.1.0/24")
print "Netmask: {}".format(netblock.netmask)
print "CIDR: {}".format(netblock.cidr)
print "Broadcast: {}".format(netblock.broadcast)
#you can use list() to generate a list of ips belonging to a netblock
ip_list = list(netblock)
#and get subnets too!
list_31 = list(netblock.subnet(31))
print "list_31[:3]: {}".format(list_31[:3])
#And we can work through the list of subnets
for subnet in list_31[:10]:
addresses = [ str(x) for x in list(subnet) ]
print "Addresses belonging to {}: {}".format(subnet,addresses)