-
Notifications
You must be signed in to change notification settings - Fork 180
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cd72113
commit e5fa38d
Showing
1 changed file
with
24 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
class HostPortParseException(Exception): | ||
pass | ||
|
||
|
||
class HostPort: | ||
def __init__(self, host: str, port: int | None = None): | ||
self.host: str = host | ||
self.port: int | None = port | ||
|
||
@classmethod | ||
def parse(cls, s: str) -> "HostPort": | ||
splid = s.split(":") | ||
if len(splid) == 1: | ||
return cls(splid[0]) | ||
elif len(splid) == 2: | ||
try: | ||
return cls(splid[0], int(splid[1])) | ||
except ValueError: | ||
HostPortParseException("Invalid port") | ||
else: | ||
raise HostPortParseException("Invalid format") | ||
|
||
def __str__(self): | ||
return f"{self.host}:{self.port}" if self.port is not None else self.host |