From c5814988c1cd5c885ad0617b075c9add002c84de Mon Sep 17 00:00:00 2001 From: RossTheRoss Date: Sun, 7 Mar 2021 15:27:37 -0600 Subject: aaaaaaaaaaaaaaaaaaaaa --- csci4131/hw4/strap012/strap012.py | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) (limited to 'csci4131') diff --git a/csci4131/hw4/strap012/strap012.py b/csci4131/hw4/strap012/strap012.py index 862ff7c..71d0762 100644 --- a/csci4131/hw4/strap012/strap012.py +++ b/csci4131/hw4/strap012/strap012.py @@ -12,25 +12,22 @@ METHOD_NOT_ALLOWED = 'HTTP/1.1 405 METHOD NOT ALLOWED{}Allow: GET, HEAD, POST { OK = 'HTTP/1.1 200 OK{}{}{}'.format(CRLF, CRLF, CRLF) # head request only def getContents(type, file): - returnValue = "" + returnValue = "".encode() try: - content = open(file, 'r') + content = open(file, 'rb') except FileNotFoundError: getContents(type, "404.html") except PermissionError: getContents(type, "403.html") else: - if type == "HEAD": - returnValue = OK - elif type == "GET": - try: - returnValue = OK + content.read() + "{}{}".format(CRLF, CRLF) - except: - content.close() + if type == "HEAD" or "GET": + returnValue.join(OK.encode()) + if type == "GET": + returnValue.join([content.read(), "{}{}".format(CRLF, CRLF).encode()]) elif type == "POST": print("B") else: - returnValue = METHOD_NOT_ALLOWED + returnValue.join(METHOD_NOT_ALLOWED.encode()) content.close() return returnValue @@ -41,7 +38,7 @@ def client_recv(client_sock, client_addr): data = data.split("\n") request = data[0].split(" ") want = getContents(request[0], request[1][1:]) - client_sock.send(bytes(want, 'utf-8')) + client_sock.send(want) client_sock.shutdown(1) client_sock.close() -- cgit v1.2.3