Added feature for validating addresses

This commit is contained in:
2026-02-28 03:07:33 +01:00
parent 2ab4cfce4e
commit f89b6445ff
3 changed files with 53 additions and 2 deletions
+3
View File
@@ -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.
## Changelog
### Version 0.2.1
- Added feature for validating addresses.
### Version 0.2.0
- Removed all the functions and classes that are now in jeb-server-utils.
+1 -1
View File
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
[project]
name = "jeb-utils"
version = "0.2.0"
version = "0.2.1"
description = "Common utils for JEB client and server."
dependencies = ["cryptography"]
license = "Apache-2.0"
+48
View File
@@ -14,6 +14,8 @@
import os
from typing import Tuple, Literal, Sequence
import ipaddress
import socket
__all__ = [
'is_number',
@@ -154,3 +156,49 @@ def create_file_if_not_exists(path: str, content: bytes = b''):
with open(path, 'wb') as f:
f.write(content)
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)