mirror of https://github.com/huashengdun/webssh
Use general method to validate ipaddress
parent
9aebb6e4e3
commit
66ebe2ceb2
|
@ -1,8 +1,8 @@
|
||||||
import unittest
|
import unittest
|
||||||
|
|
||||||
from webssh.utils import (
|
from webssh.utils import (
|
||||||
is_valid_ipv4_address, is_valid_ipv6_address, is_valid_port,
|
is_valid_ip_address, is_valid_port, is_valid_hostname,
|
||||||
is_valid_hostname, to_str, to_bytes, to_int
|
to_str, to_bytes, to_int
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@ -26,17 +26,15 @@ class TestUitls(unittest.TestCase):
|
||||||
self.assertEqual(to_int('22'), 22)
|
self.assertEqual(to_int('22'), 22)
|
||||||
self.assertEqual(to_int(' 22 '), 22)
|
self.assertEqual(to_int(' 22 '), 22)
|
||||||
|
|
||||||
def test_is_valid_ipv4_address(self):
|
def test_is_valid_ip_address(self):
|
||||||
self.assertFalse(is_valid_ipv4_address('127.0.0'))
|
self.assertFalse(is_valid_ip_address('127.0.0'))
|
||||||
self.assertFalse(is_valid_ipv4_address(b'127.0.0'))
|
self.assertFalse(is_valid_ip_address(b'127.0.0'))
|
||||||
self.assertTrue(is_valid_ipv4_address('127.0.0.1'))
|
self.assertTrue(is_valid_ip_address('127.0.0.1'))
|
||||||
self.assertTrue(is_valid_ipv4_address(b'127.0.0.1'))
|
self.assertTrue(is_valid_ip_address(b'127.0.0.1'))
|
||||||
|
self.assertFalse(is_valid_ip_address('abc'))
|
||||||
def test_is_valid_ipv6_address(self):
|
self.assertFalse(is_valid_ip_address(b'abc'))
|
||||||
self.assertFalse(is_valid_ipv6_address('abc'))
|
self.assertTrue(is_valid_ip_address('::1'))
|
||||||
self.assertFalse(is_valid_ipv6_address(b'abc'))
|
self.assertTrue(is_valid_ip_address(b'::1'))
|
||||||
self.assertTrue(is_valid_ipv6_address('::1'))
|
|
||||||
self.assertTrue(is_valid_ipv6_address(b'::1'))
|
|
||||||
|
|
||||||
def test_is_valid_port(self):
|
def test_is_valid_port(self):
|
||||||
self.assertTrue(is_valid_port(80))
|
self.assertTrue(is_valid_port(80))
|
||||||
|
|
|
@ -12,8 +12,8 @@ import tornado.web
|
||||||
from tornado.ioloop import IOLoop
|
from tornado.ioloop import IOLoop
|
||||||
from webssh.settings import swallow_http_errors
|
from webssh.settings import swallow_http_errors
|
||||||
from webssh.utils import (
|
from webssh.utils import (
|
||||||
is_valid_ipv4_address, is_valid_ipv6_address, is_valid_port,
|
is_valid_ip_address, is_valid_port, is_valid_hostname,
|
||||||
is_valid_hostname, to_bytes, to_str, to_int, UnicodeType
|
to_bytes, to_str, to_int, UnicodeType
|
||||||
)
|
)
|
||||||
from webssh.worker import Worker, recycle_worker, workers
|
from webssh.worker import Worker, recycle_worker, workers
|
||||||
|
|
||||||
|
@ -149,8 +149,7 @@ class IndexHandler(MixinHandler, tornado.web.RequestHandler):
|
||||||
|
|
||||||
def get_hostname(self):
|
def get_hostname(self):
|
||||||
value = self.get_value('hostname')
|
value = self.get_value('hostname')
|
||||||
if not (is_valid_hostname(value) | is_valid_ipv4_address(value) |
|
if not (is_valid_hostname(value) | is_valid_ip_address(value)):
|
||||||
is_valid_ipv6_address(value)):
|
|
||||||
raise InvalidValueError('Invalid hostname: {}'.format(value))
|
raise InvalidValueError('Invalid hostname: {}'.format(value))
|
||||||
return value
|
return value
|
||||||
|
|
||||||
|
|
|
@ -30,20 +30,11 @@ def to_int(string):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
def is_valid_ipv4_address(ipstr):
|
def is_valid_ip_address(ipstr):
|
||||||
ipstr = to_str(ipstr)
|
ipstr = to_str(ipstr)
|
||||||
try:
|
try:
|
||||||
ipaddress.IPv4Address(ipstr)
|
ipaddress.ip_address(ipstr)
|
||||||
except ipaddress.AddressValueError:
|
except ValueError:
|
||||||
return False
|
|
||||||
return True
|
|
||||||
|
|
||||||
|
|
||||||
def is_valid_ipv6_address(ipstr):
|
|
||||||
ipstr = to_str(ipstr)
|
|
||||||
try:
|
|
||||||
ipaddress.IPv6Address(ipstr)
|
|
||||||
except ipaddress.AddressValueError:
|
|
||||||
return False
|
return False
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue