This commit is contained in:
jxxghp 2024-06-07 08:01:23 +08:00
parent 03d6834471
commit c0c787f7ed

View File

@ -609,23 +609,21 @@ class StringUtils:
""" """
if not address: if not address:
return None, None return None, None
# 去掉末尾的/
address = address.rstrip("/")
if prefix and not address.startswith("http"): if prefix and not address.startswith("http"):
# 如果需要包含协议前缀,但地址不包含协议前缀,则添加 # 如果需要包含协议前缀,但地址不包含协议前缀,则添加
address = "http://" + address address = "http://" + address
elif not prefix and address.startswith("http"): elif not prefix and address.startswith("http"):
# 如果不需要包含协议前缀,但地址包含协议前缀,则去掉 # 如果不需要包含协议前缀,但地址包含协议前缀,则去掉
address = address.split("://")[-1] address = address.split("://")[-1]
# 去掉末尾的/
address = address.rstrip("/")
# 拆分域名和端口号 # 拆分域名和端口号
parts = address.split(":") parts = address.split(":")
if len(parts) > 3: if len(parts) > 3:
# 处理不希望包含多个冒号的情况(除了协议后的冒号) # 处理不希望包含多个冒号的情况(除了协议后的冒号)
return None, None return None, None
# 不含端口地址 # 不含端口地址
domain = ":".join(parts[:-1]) domain = ":".join(parts[:-1]).rstrip('/')
if domain.endswith("/"):
domain = domain[:-1]
# 端口号 # 端口号
try: try:
port = int(parts[-1]) port = int(parts[-1])