def test_get_my_ip(self):
httpretty.register_uri(httpretty.GET, "http://test.com/",
body=json.dumps({
"address": "fd2b:1c1b:3641:1cd8::",
"proto": "ipv6"}),
content_type='text/json')
res = get_my_ip('http://test.com/')
self.assertIsInstance(res, ipaddress._BaseAddress)
self.assertEqual(ipaddress.ip_address(u"fd2b:1c1b:3641:1cd8::"), res)
httpretty.register_uri(httpretty.GET, "http://test.com/",
body=json.dumps({
"address": "49.20.57.31",
"proto": "ipv4"}),
content_type='text/json')
res = get_my_ip('http://test.com/')
self.assertIsInstance(res, ipaddress._BaseAddress)
self.assertEqual(ipaddress.ip_address(u"49.20.57.31"), res)
python类_BaseAddress()的实例源码
def testMissingAddressVersion(self):
class Broken(ipaddress._BaseAddress):
pass
broken = Broken('127.0.0.1')
with self.assertRaisesRegex(NotImplementedError, "Broken.*version"):
broken.version
def testMissingAddressVersion(self):
class Broken(ipaddress._BaseAddress):
pass
broken = Broken('127.0.0.1')
with self.assertRaisesRegex(NotImplementedError, "Broken.*version"):
broken.version
def test_get_my_ip(self):
res = get_my_ip()
self.assertIsInstance(res, ipaddress._BaseAddress)
def testMissingAddressVersion(self):
class Broken(ipaddress._BaseAddress):
pass
broken = Broken('127.0.0.1')
with self.assertRaisesRegex(NotImplementedError, "Broken.*version"):
broken.version
def process_value_type(self, sentinel, value, resource):
if self.vtype == 'normalize' and isinstance(value, six.string_types):
return sentinel, value.strip().lower()
elif self.vtype == 'expr':
return sentinel, self.get_resource_value(value, resource)
elif self.vtype == 'integer':
try:
value = int(value.strip())
except ValueError:
value = 0
elif self.vtype == 'size':
try:
return sentinel, len(value)
except TypeError:
return sentinel, 0
elif self.vtype == 'swap':
return value, sentinel
elif self.vtype == 'age':
if not isinstance(sentinel, datetime.datetime):
sentinel = datetime.datetime.now(tz=tzutc()) - timedelta(sentinel)
if not isinstance(value, datetime.datetime):
# EMR bug when testing ages in EMR. This is due to
# EMR not having more functionality.
try:
value = parse(value, default=datetime.datetime.now(tz=tzutc()))
except (AttributeError, TypeError, ValueError):
value = 0
# Reverse the age comparison, we want to compare the value being
# greater than the sentinel typically. Else the syntax for age
# comparisons is intuitively wrong.
return value, sentinel
elif self.vtype == 'cidr':
s = parse_cidr(sentinel)
v = parse_cidr(value)
if (isinstance(s, ipaddress._BaseAddress) and isinstance(v, ipaddress._BaseNetwork)):
return v, s
return s, v
elif self.vtype == 'cidr_size':
cidr = parse_cidr(value)
if cidr:
return sentinel, cidr.prefixlen
return sentinel, 0
# Allows for expiration filtering, for events in the future as opposed
# to events in the past which age filtering allows for.
elif self.vtype == 'expiration':
if not isinstance(sentinel, datetime.datetime):
sentinel = datetime.datetime.now(tz=tzutc()) + timedelta(sentinel)
if not isinstance(value, datetime.datetime):
try:
value = parse(value, default=datetime.datetime.now(tz=tzutc()))
except (AttributeError, TypeError, ValueError):
value = 0
return sentinel, value
return sentinel, value