Last active
December 29, 2020 01:19
-
-
Save yonderbread/27f5ca84b9125b961c245650f19cb321 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import socket | |
class Webserver: | |
def __init__(self, host: str = '0.0.0.0', port: int = 8080): | |
self.host = host | |
self.port = port | |
self._socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
self._socket.bind((self.host, self.port)) | |
self._socket.listen(1) | |
@staticmethod | |
def _send(clientSocket, data: str): | |
sent = clientSocket.send(data.encode()) | |
return sent | |
def run(self): | |
conn, addr = self._socket.accept() | |
print(conn.recv(1024).decode('utf-8')) | |
self._send(conn, 'HTTP/1.0 200 OK\n\nHello world!') | |
conn.close() | |
self._socket.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment