Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
W
webssh
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
郑天保
webssh
Commits
66ebe2ce
Commit
66ebe2ce
authored
Oct 13, 2018
by
Sheng
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Use general method to validate ipaddress
parent
9aebb6e4
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
17 additions
and
29 deletions
+17
-29
test_utils.py
tests/test_utils.py
+11
-13
handler.py
webssh/handler.py
+3
-4
utils.py
webssh/utils.py
+3
-12
No files found.
tests/test_utils.py
View file @
66ebe2ce
import
unittest
import
unittest
from
webssh.utils
import
(
from
webssh.utils
import
(
is_valid_ip
v4_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):
...
@@ -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
))
...
...
webssh/handler.py
View file @
66ebe2ce
...
@@ -12,8 +12,8 @@ import tornado.web
...
@@ -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_ip
v4_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):
...
@@ -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
...
...
webssh/utils.py
View file @
66ebe2ce
...
@@ -30,20 +30,11 @@ def to_int(string):
...
@@ -30,20 +30,11 @@ def to_int(string):
pass
pass
def
is_valid_ip
v4
_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
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment