This commit is contained in:
2021-03-27 16:30:53 +01:00
parent fdbd865889
commit 3615c56152
14 changed files with 280 additions and 110 deletions

View File

@@ -13,19 +13,30 @@ sockets: Dict[str, HTTPClient] = {}
def create(command: str, url: str, port):
"""
Create a corresponding Command instance of the specified HTTP `command` with the specified `url` and `port`.
@param command: The command type to create
@param url: The url for the command
@param port: The port for the command
"""
uri = parser.get_uri(url)
if command == "GET":
return GetCommand(url, port)
return GetCommand(uri, port)
elif command == "HEAD":
return HeadCommand(url, port)
return HeadCommand(uri, port)
elif command == "POST":
return PostCommand(url, port)
return PostCommand(uri, port)
elif command == "PUT":
return PutCommand(url, port)
return PutCommand(uri, port)
else:
raise ValueError()
class AbstractCommand(ABC):
"""
A class representing the command for sending an HTTP command.
"""
uri: str
host: str
path: str
@@ -111,6 +122,9 @@ class AbstractCommand(ABC):
class AbstractWithBodyCommand(AbstractCommand, ABC):
"""
The building block for creating an HTTP message for an HTTP command with a body.
"""
def _build_message(self, message: str) -> bytes:
body = input(f"Enter {self.command} data: ").encode(FORMAT)
@@ -127,12 +141,19 @@ class AbstractWithBodyCommand(AbstractCommand, ABC):
class HeadCommand(AbstractCommand):
"""
A Command for sending a `HEAD` message.
"""
@property
def command(self):
return "HEAD"
class GetCommand(AbstractCommand):
"""
A Command for sending a `GET` message.
"""
def __init__(self, uri: str, port, dir=None):
super().__init__(uri, port)
@@ -160,12 +181,20 @@ class GetCommand(AbstractCommand):
class PostCommand(AbstractWithBodyCommand):
"""
A command for sending a `POST` command.
"""
@property
def command(self):
return "POST"
class PutCommand(AbstractWithBodyCommand):
"""
A command for sending a `PUT` command.
"""
@property
def command(self):
return "PUT"