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
90397715
Commit
90397715
authored
Aug 21, 2018
by
Sheng
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Optimized is_valid_hostname
parent
9a49c9e5
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
7 additions
and
5 deletions
+7
-5
utils.py
webssh/utils.py
+7
-5
No files found.
webssh/utils.py
View file @
90397715
import
ipaddress
import
ipaddress
import
re
import
re
try
:
try
:
from
types
import
UnicodeType
from
types
import
UnicodeType
except
ImportError
:
except
ImportError
:
UnicodeType
=
str
UnicodeType
=
str
numeric
=
re
.
compile
(
r'[0-9]+$'
)
allowed
=
re
.
compile
(
r'(?!-)[a-z0-9-]{1,63}(?<!-)$'
,
re
.
IGNORECASE
)
def
to_str
(
bstr
,
encoding
=
'utf-8'
):
def
to_str
(
bstr
,
encoding
=
'utf-8'
):
if
isinstance
(
bstr
,
bytes
):
if
isinstance
(
bstr
,
bytes
):
return
bstr
.
decode
(
encoding
)
return
bstr
.
decode
(
encoding
)
...
@@ -43,17 +46,16 @@ def is_valid_port(port):
...
@@ -43,17 +46,16 @@ def is_valid_port(port):
def
is_valid_hostname
(
hostname
):
def
is_valid_hostname
(
hostname
):
if
hostname
[
-
1
]
==
"."
:
if
hostname
[
-
1
]
==
'.'
:
# strip exactly one dot from the right, if present
# strip exactly one dot from the right, if present
hostname
=
hostname
[:
-
1
]
hostname
=
hostname
[:
-
1
]
if
len
(
hostname
)
>
253
:
if
len
(
hostname
)
>
253
:
return
False
return
False
labels
=
hostname
.
split
(
"."
)
labels
=
hostname
.
split
(
'.'
)
# the TLD must be not all-numeric
# the TLD must be not all-numeric
if
re
.
match
(
r"[0-9]+$"
,
labels
[
-
1
]):
if
numeric
.
match
(
labels
[
-
1
]):
return
False
return
False
allowed
=
re
.
compile
(
r"(?!-)[a-z0-9-]{1,63}(?<!-)$"
,
re
.
IGNORECASE
)
return
all
(
allowed
.
match
(
label
)
for
label
in
labels
)
return
all
(
allowed
.
match
(
label
)
for
label
in
labels
)
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