Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 82 additions & 20 deletions http_server.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import socket
import os
import sys
import socket
import mimetypes
import traceback
from pathlib import Path

def response_ok(body=b"This is a minimal response", mimetype=b"text/plain"):
"""
Expand All @@ -20,20 +23,30 @@ def response_ok(body=b"This is a minimal response", mimetype=b"text/plain"):
"""

# TODO: Implement response_ok
return b""
return b"\r\n".join([
b"HTTP/1.1 200 OK",
b"Content-Type: " + mimetype,
b"",
body])

def response_method_not_allowed():
"""Returns a 405 Method Not Allowed response"""

# TODO: Implement response_method_not_allowed
return b""
return b"\r\n".join([
b"HTTP/1.1 405 Method Not Allowed",
b"",
b"You can't do that on this server!"
])


def response_not_found():
"""Returns a 404 Not Found response"""

# TODO: Implement response_not_found
return b""
return b"\r\n".join([
b"HTTP/1.1 404 Not Found"
])


def parse_request(request):
Expand All @@ -45,7 +58,12 @@ def parse_request(request):
"""

# TODO: implement parse_request
return ""
method, path, version = request.split("\r\n")[0].split(" ")

if method != "GET":
raise NotImplementedError

return path

def response_path(path):
"""
Expand Down Expand Up @@ -85,10 +103,43 @@ def response_path(path):
# If the path is "make_time.py", then you may OPTIONALLY return the
# result of executing `make_time.py`. But you need only return the
# CONTENTS of `make_time.py`.

content = b"not implemented"
mime_type = b"not implemented"


#root_dir = os.path.dirname(os.path.realpath(__file__)) + "/webroot/"
root_dir = os.path.dirname(os.path.realpath(__file__))

if path =="" or path =="/":
with open(root_dir + "/index.html", "r") as f:
content = f.read().encode()
content += "\r\n".join(os.listdir(root_dir + "/webroot/")).encode()
elif path.endswith(".py"):
content = open(root_dir + "/make_time.py").read()
exec(content)
content = content.encode()
else:
whole_path = Path(root_dir + "/webroot/" + path)

try:
assert os.path.exists(whole_path)
except AssertionError:
raise NameError
else:
if os.path.isfile(whole_path):
with open(whole_path, "rb") as f:
content = f.read()
else:
content = "\r\n".join(os.listdir(whole_path)).encode()

mime_type = b"text/plain"
if path.endswith(".html") or path.endswith('.htm') or path.endswith('') or path.endswith('/') or path.endswith('/') :
mime_type = b"text/html"
if path.endswith(".txt"):
mime_type = b"text/plain"
if path.endswith(".png"):
mime_type = b"image/png"
if path.endswith(".jpg"):
mime_type = b"image/jpeg"

return content, mime_type


Expand Down Expand Up @@ -118,22 +169,33 @@ def server(log_buffer=sys.stderr):

print("Request received:\n{}\n\n".format(request))

# TODO: Use parse_request to retrieve the path from the request.
try:
path = parse_request(request)

content, mime_type = response_path(path)
# TODO: Use parse_request to retrieve the path from the request.

# TODO: Use response_path to retrieve the content and the mimetype,
# based on the request path.
# TODO: Use response_path to retrieve the content and the mimetype,
# based on the request path.

# TODO; If parse_request raised a NotImplementedError, then let
# response be a method_not_allowed response. If response_path raised
# a NameError, then let response be a not_found response. Else,
# use the content and mimetype from response_path to build a
# response_ok.
response = response_ok(
body=b"Welcome to my web server",
mimetype=b"text/plain"
)
# TODO; If parse_request raised a NotImplementedError, then let
# response be a method_not_allowed response. If response_path raised
# a NameError, then let response be a not_found response. Else,
# use the content and mimetype from response_path to build a
# response_ok.
response = response_ok(
body= content,
mimetype= mime_type
)

except NotImplementedError:
response = response_method_not_allowed()

except NameError:
response = response_not_found()

conn.sendall(response)

except:
traceback.print_exc()
finally:
Expand Down
18 changes: 18 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<!DOCTYPE html>
<html>
<head>
<title>Internet Programming in Python: CGI</title>
</head>
<body>
<h1>Internet Programming in Python</h1>
<h2>socket-http-server assignment</h2>
<ol>
<li><a href="a_web_page.html">a_web_page.html</a></li>
<li><a href="/images/JPEG_example.jpg">JPEG_example</a></li>
<li><a href="sample.txt">sample.txt</a></li>
<li><a href="/images/sample_1.png">sample_1.png</a></li>
<li><a href="/images/Sample_Scene_Balls.jpg">Sample_Scene_Balls.jpg</a></li>
<li><a href="cgi-bin/make_time.py">make_time.py</a></li>
</ol>
</body>
</html>
File renamed without changes.