本文整理匯總了Python中gluon.utils.is_valid_ip_address方法的典型用法代碼示例。如果您正苦於以下問題:Python utils.is_valid_ip_address方法的具體用法?Python utils.is_valid_ip_address怎麽用?Python utils.is_valid_ip_address使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類gluon.utils
的用法示例。
在下文中一共展示了utils.is_valid_ip_address方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_is_valid_ip_address
# 需要導入模塊: from gluon import utils [as 別名]
# 或者: from gluon.utils import is_valid_ip_address [as 別名]
def test_is_valid_ip_address(self):
# IPv4
# False
# self.assertEqual(is_valid_ip_address('127.0'), False) # Fail with AppVeyor?? should pass
self.assertEqual(is_valid_ip_address('unknown'), False)
self.assertEqual(is_valid_ip_address(''), False)
# True
self.assertEqual(is_valid_ip_address('127.0.0.1'), True)
self.assertEqual(is_valid_ip_address('localhost'), True)
self.assertEqual(is_valid_ip_address('::1'), True)
# IPv6
# True
# Compressed
self.assertEqual(is_valid_ip_address('::ffff:7f00:1'), True) # IPv6 127.0.0.1 compressed
self.assertEqual(is_valid_ip_address('2001:660::1'), True)
# Expanded
self.assertEqual(is_valid_ip_address('0:0:0:0:0:ffff:7f00:1'), True) # IPv6 127.0.0.1 expanded
self.assertEqual(is_valid_ip_address('2607:fa48:6d50:69f1:21f:3cff:fe9d:9be3'), True) # Any address
# False
# self.assertEqual(is_valid_ip_address('2607:fa48:6d50:69f1:21f:3cff:fe9d:'), False) # Any address with mistake
# The above pass locally but fail with AppVeyor
# TODO: def test_is_loopback_ip_address(self):
# TODO: def test_getipaddrinfo(self):
示例2: get_client
# 需要導入模塊: from gluon import utils [as 別名]
# 或者: from gluon.utils import is_valid_ip_address [as 別名]
def get_client(env):
"""
Guesses the client address from the environment variables
First tries 'http_x_forwarded_for', secondly 'remote_addr'
if all fails, assume '127.0.0.1' or '::1' (running locally)
"""
eget = env.get
g = regex_client.search(eget('http_x_forwarded_for', ''))
client = (g.group() or '').split(',')[0] if g else None
if client in (None, '', 'unknown'):
g = regex_client.search(eget('remote_addr', ''))
if g:
client = g.group()
elif env.http_host.startswith('['): # IPv6
client = '::1'
else:
client = '127.0.0.1' # IPv4
if not is_valid_ip_address(client):
raise HTTP(400, "Bad Request (request.client=%s)" % client)
return client