Use general method to validate ipaddress

This commit is contained in:
Sheng
2018-10-13 09:47:15 +08:00
parent 9aebb6e4e3
commit 66ebe2ceb2
3 changed files with 17 additions and 29 deletions
+3 -4
View File
@@ -12,8 +12,8 @@ import tornado.web
from tornado.ioloop import IOLoop
from webssh.settings import swallow_http_errors
from webssh.utils import (
is_valid_ipv4_address, is_valid_ipv6_address, is_valid_port,
is_valid_hostname, to_bytes, to_str, to_int, UnicodeType
is_valid_ip_address, is_valid_port, is_valid_hostname,
to_bytes, to_str, to_int, UnicodeType
)
from webssh.worker import Worker, recycle_worker, workers
@@ -149,8 +149,7 @@ class IndexHandler(MixinHandler, tornado.web.RequestHandler):
def get_hostname(self):
value = self.get_value('hostname')
if not (is_valid_hostname(value) | is_valid_ipv4_address(value) |
is_valid_ipv6_address(value)):
if not (is_valid_hostname(value) | is_valid_ip_address(value)):
raise InvalidValueError('Invalid hostname: {}'.format(value))
return value
+3 -12
View File
@@ -30,20 +30,11 @@ def to_int(string):
pass
def is_valid_ipv4_address(ipstr):
def is_valid_ip_address(ipstr):
ipstr = to_str(ipstr)
try:
ipaddress.IPv4Address(ipstr)
except ipaddress.AddressValueError:
return False
return True
def is_valid_ipv6_address(ipstr):
ipstr = to_str(ipstr)
try:
ipaddress.IPv6Address(ipstr)
except ipaddress.AddressValueError:
ipaddress.ip_address(ipstr)
except ValueError:
return False
return True