Added feature for validating addresses
This commit is contained in:
@@ -77,6 +77,9 @@ pip install jeb-utils --index-url https://repo.jcloud-services.ddns.net/simple/
|
|||||||
- `create_file_if_not_exists`: Creates a file if it does not exist.
|
- `create_file_if_not_exists`: Creates a file if it does not exist.
|
||||||
|
|
||||||
## Changelog
|
## Changelog
|
||||||
|
### Version 0.2.1
|
||||||
|
- Added feature for validating addresses.
|
||||||
|
|
||||||
### Version 0.2.0
|
### Version 0.2.0
|
||||||
- Removed all the functions and classes that are now in jeb-server-utils.
|
- Removed all the functions and classes that are now in jeb-server-utils.
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
|
|||||||
|
|
||||||
[project]
|
[project]
|
||||||
name = "jeb-utils"
|
name = "jeb-utils"
|
||||||
version = "0.2.0"
|
version = "0.2.1"
|
||||||
description = "Common utils for JEB client and server."
|
description = "Common utils for JEB client and server."
|
||||||
dependencies = ["cryptography"]
|
dependencies = ["cryptography"]
|
||||||
license = "Apache-2.0"
|
license = "Apache-2.0"
|
||||||
+49
-1
@@ -14,6 +14,8 @@
|
|||||||
|
|
||||||
import os
|
import os
|
||||||
from typing import Tuple, Literal, Sequence
|
from typing import Tuple, Literal, Sequence
|
||||||
|
import ipaddress
|
||||||
|
import socket
|
||||||
|
|
||||||
__all__ = [
|
__all__ = [
|
||||||
'is_number',
|
'is_number',
|
||||||
@@ -153,4 +155,50 @@ def create_file_if_not_exists(path: str, content: bytes = b''):
|
|||||||
if not os.path.exists(path):
|
if not os.path.exists(path):
|
||||||
with open(path, 'wb') as f:
|
with open(path, 'wb') as f:
|
||||||
f.write(content)
|
f.write(content)
|
||||||
f.close()
|
f.close()
|
||||||
|
|
||||||
|
def is_valid_host(host: str) -> bool:
|
||||||
|
'''
|
||||||
|
Checks whether the host is a valid IP address.
|
||||||
|
|
||||||
|
:param host: The host
|
||||||
|
:type host: str
|
||||||
|
|
||||||
|
:return: ``True`` if the host is a valid IP address, otherwise ``False``
|
||||||
|
:rtype: bool
|
||||||
|
'''
|
||||||
|
try:
|
||||||
|
ipaddress.ip_address(host)
|
||||||
|
return True
|
||||||
|
except ValueError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
def is_valid_port(port: str) -> bool:
|
||||||
|
'''
|
||||||
|
Checks whether the port is a valid port number.
|
||||||
|
|
||||||
|
:param port: The port
|
||||||
|
:type port: str
|
||||||
|
|
||||||
|
:return: ``True`` if the port is a valid port number, otherwise ``False``
|
||||||
|
:rtype: bool
|
||||||
|
'''
|
||||||
|
if not port.isdigit():
|
||||||
|
return False
|
||||||
|
port_num = int(port)
|
||||||
|
return 1 <= port_num <= 65535
|
||||||
|
|
||||||
|
def validate_address_port(addr: str) -> bool:
|
||||||
|
if ':' not in addr:
|
||||||
|
return False
|
||||||
|
|
||||||
|
if addr.startswith('['):
|
||||||
|
try:
|
||||||
|
host, port = addr.rsplit(']:', 1)
|
||||||
|
host = host[1:]
|
||||||
|
except ValueError:
|
||||||
|
return False
|
||||||
|
else:
|
||||||
|
host, port = addr.rsplit(':', 1)
|
||||||
|
|
||||||
|
return is_valid_host(host) and is_valid_port(port)
|
||||||
Reference in New Issue
Block a user