diff options
| author | UltraQbik <no1skill@yandex.ru> | 2024-08-20 18:45:56 +0300 |
|---|---|---|
| committer | UltraQbik <no1skill@yandex.ru> | 2024-08-20 18:45:56 +0300 |
| commit | d68eccbe757af8ab55f1f5424270fccc61b1067a (patch) | |
| tree | 88ff1d6849851d9606119614524540130b1efe36 /main.py | |
| parent | 7b804f823d3c1a96192f5d9d1d0ae286f47579b7 (diff) | |
| download | httpy-d68eccbe757af8ab55f1f5424270fccc61b1067a.tar.gz httpy-d68eccbe757af8ab55f1f5424270fccc61b1067a.zip | |
Working website
Diffstat (limited to 'main.py')
| -rw-r--r-- | main.py | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/main.py b/main.py index c49133f..d718238 100644 --- a/main.py +++ b/main.py @@ -165,6 +165,33 @@ class HTTPServer: # if it is -> return file from that path async with aiofiles.open(PATH_MAP[request.path]["path"], "rb") as f: data = await f.read() + HTTPServer._send(client, 200, data) + + @staticmethod + def _send(client: socket.socket, response: int, data: bytes, headers: dict[str, str] = None): + """ + Sends client response code + headers + data + :param client: client + :param response: response code + :param data: data + :param headers: headers to include + """ + + if headers is None: + headers = dict() + + byte_header = bytearray() + for key, value in headers.items(): + byte_header += f"{key}: {value}\r\n".encode("ascii") + + client.sendall( + b'HTTP/1.1 ' + + get_response_code(response) + + b'\r\n' + + byte_header + # if empty, we'll just get b'\r\n\r\n' + b'\r\n' + + data + ) def _close_client(self, client: socket.socket): """ |