aboutsummaryrefslogtreecommitdiffstats
path: root/csci4131/hw4/strap012
diff options
context:
space:
mode:
authorRossTheRoss <mstrapp@protonmail.com>2021-03-07 15:27:37 -0600
committerRossTheRoss <mstrapp@protonmail.com>2021-03-07 15:27:37 -0600
commitc5814988c1cd5c885ad0617b075c9add002c84de (patch)
tree06b157e3acf5a49e587436809c5f62d12b071f08 /csci4131/hw4/strap012
parentaaaaaaaaaaaaaaaaaaaaaaaaa (diff)
downloadhomework-c5814988c1cd5c885ad0617b075c9add002c84de.tar
homework-c5814988c1cd5c885ad0617b075c9add002c84de.tar.gz
homework-c5814988c1cd5c885ad0617b075c9add002c84de.tar.bz2
homework-c5814988c1cd5c885ad0617b075c9add002c84de.tar.lz
homework-c5814988c1cd5c885ad0617b075c9add002c84de.tar.xz
homework-c5814988c1cd5c885ad0617b075c9add002c84de.tar.zst
homework-c5814988c1cd5c885ad0617b075c9add002c84de.zip
aaaaaaaaaaaaaaaaaaaaa
Diffstat (limited to 'csci4131/hw4/strap012')
-rw-r--r--csci4131/hw4/strap012/strap012.py19
1 files changed, 8 insertions, 11 deletions
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()