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
88405edd
Commit
88405edd
authored
Oct 10, 2018
by
Sheng
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added to_int to utils
parent
de0fda1a
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
22 additions
and
16 deletions
+22
-16
test_utils.py
tests/test_utils.py
+10
-2
handler.py
webssh/handler.py
+5
-14
utils.py
webssh/utils.py
+7
-0
No files found.
tests/test_utils.py
View file @
88405edd
import
unittest
import
unittest
from
webssh.utils
import
(
is_valid_ipv4_address
,
is_valid_ipv6_address
,
from
webssh.utils
import
(
is_valid_port
,
is_valid_hostname
,
to_str
,
to_bytes
)
is_valid_ipv4_address
,
is_valid_ipv6_address
,
is_valid_port
,
is_valid_hostname
,
to_str
,
to_bytes
,
to_int
)
class
TestUitls
(
unittest
.
TestCase
):
class
TestUitls
(
unittest
.
TestCase
):
...
@@ -18,6 +20,12 @@ class TestUitls(unittest.TestCase):
...
@@ -18,6 +20,12 @@ class TestUitls(unittest.TestCase):
self
.
assertEqual
(
to_bytes
(
b
),
b
)
self
.
assertEqual
(
to_bytes
(
b
),
b
)
self
.
assertEqual
(
to_bytes
(
u
),
b
)
self
.
assertEqual
(
to_bytes
(
u
),
b
)
def
test_to_int
(
self
):
self
.
assertEqual
(
to_int
(
''
),
None
)
self
.
assertEqual
(
to_int
(
None
),
None
)
self
.
assertEqual
(
to_int
(
'22'
),
22
)
self
.
assertEqual
(
to_int
(
' 22 '
),
22
)
def
test_is_valid_ipv4_address
(
self
):
def
test_is_valid_ipv4_address
(
self
):
self
.
assertFalse
(
is_valid_ipv4_address
(
'127.0.0'
))
self
.
assertFalse
(
is_valid_ipv4_address
(
'127.0.0'
))
self
.
assertFalse
(
is_valid_ipv4_address
(
b
'127.0.0'
))
self
.
assertFalse
(
is_valid_ipv4_address
(
b
'127.0.0'
))
...
...
webssh/handler.py
View file @
88405edd
...
@@ -13,7 +13,7 @@ from tornado.ioloop import IOLoop
...
@@ -13,7 +13,7 @@ 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_ipv4_address
,
is_valid_ipv6_address
,
is_valid_port
,
is_valid_hostname
,
to_bytes
,
to_str
,
UnicodeType
is_valid_hostname
,
to_bytes
,
to_str
,
to_int
,
UnicodeType
)
)
from
webssh.worker
import
Worker
,
recycle_worker
,
workers
from
webssh.worker
import
Worker
,
recycle_worker
,
workers
...
@@ -52,12 +52,8 @@ class MixinHandler(object):
...
@@ -52,12 +52,8 @@ class MixinHandler(object):
return
# suppose this app doesn't run after an nginx server
return
# suppose this app doesn't run after an nginx server
if
is_valid_ipv4_address
(
ip
)
or
is_valid_ipv6_address
(
ip
):
if
is_valid_ipv4_address
(
ip
)
or
is_valid_ipv6_address
(
ip
):
try
:
port
=
to_int
(
port
)
port
=
int
(
port
)
if
port
and
is_valid_port
(
port
):
except
(
TypeError
,
ValueError
):
pass
else
:
if
is_valid_port
(
port
):
return
(
ip
,
port
)
return
(
ip
,
port
)
logging
.
warning
(
'Bad nginx configuration.'
)
logging
.
warning
(
'Bad nginx configuration.'
)
...
@@ -154,14 +150,9 @@ class IndexHandler(MixinHandler, tornado.web.RequestHandler):
...
@@ -154,14 +150,9 @@ class IndexHandler(MixinHandler, tornado.web.RequestHandler):
def
get_port
(
self
):
def
get_port
(
self
):
value
=
self
.
get_value
(
'port'
)
value
=
self
.
get_value
(
'port'
)
try
:
port
=
to_int
(
value
)
port
=
int
(
value
)
if
port
and
is_valid_port
(
port
):
except
ValueError
:
pass
else
:
if
is_valid_port
(
port
):
return
port
return
port
raise
InvalidValueError
(
'Invalid port: {}'
.
format
(
value
))
raise
InvalidValueError
(
'Invalid port: {}'
.
format
(
value
))
def
lookup_hostname
(
self
,
hostname
,
port
):
def
lookup_hostname
(
self
,
hostname
,
port
):
...
...
webssh/utils.py
View file @
88405edd
...
@@ -23,6 +23,13 @@ def to_bytes(ustr, encoding='utf-8'):
...
@@ -23,6 +23,13 @@ def to_bytes(ustr, encoding='utf-8'):
return
ustr
return
ustr
def
to_int
(
string
):
try
:
return
int
(
string
)
except
(
TypeError
,
ValueError
):
pass
def
is_valid_ipv4_address
(
ipstr
):
def
is_valid_ipv4_address
(
ipstr
):
ipstr
=
to_str
(
ipstr
)
ipstr
=
to_str
(
ipstr
)
try
:
try
:
...
...
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