- length = len(htmldata.encode("utf-8"))
- return (
- f"{proto} 200 Ok\r\n"
- f"Content-Type: text/html; charset=utf-8\r\n"
- f"Content-Length: {length:d}\r\n\r\n" + htmldata
- ).encode("utf-8")
+ try:
+ with open(htmlfile, "rb") as fl:
+ htmldata = fl.read()
+ length = len(htmldata)
+ return (
+ f"{proto} 200 Ok\r\n"
+ f"Content-Type: text/html; charset=utf-8\r\n"
+ f"Content-Length: {len(htmldata):d}\r\n\r\n"
+ ).encode("utf-8") + htmldata
+ except OSError:
+ return (
+ f"{proto} 500 File not found\r\n"
+ f"Content-Type: text/plain\r\n\r\n"
+ f'HTML file could not be opened\r\n'.encode()
+ )