From 4a66bb398ab80870134cc0619d2490fcf62ed47c Mon Sep 17 00:00:00 2001 From: sidekek Date: Fri, 1 Oct 2021 12:18:30 -0400 Subject: [PATCH] init --- __pycache__/response.cpython-39.pyc | Bin 0 -> 2170 bytes body.html | 1 + readme.txt | 3 ++ response.py | 49 ++++++++++++++++++++++++++++ server.py | 13 ++++++++ 5 files changed, 66 insertions(+) create mode 100644 __pycache__/response.cpython-39.pyc create mode 100644 body.html create mode 100644 readme.txt create mode 100644 response.py create mode 100644 server.py diff --git a/__pycache__/response.cpython-39.pyc b/__pycache__/response.cpython-39.pyc new file mode 100644 index 0000000000000000000000000000000000000000..de92817ecac009c0df86f2d765a2ccc4c9aaa1f8 GIT binary patch literal 2170 zcmZ`)OK&4Z5bo}Ic>LTAuw;`(FbA|S(k3PskWd6cfn5;bT`A!qOK3EiZq{SQGfq!? zAtUc8`3G?1U?2CN@EiKdi8~j%Cn0 z5A#XJ%R66WO2{lq{R1JN3F&_(WiHFj&kqlu_}>ayBzfj%pk7SI<6J7i1LvS6c$A>&a*;nk59B0hDnyFFs!3h287V^Ru$#;axXUPxCaAy2c~hu2WCoUMA0cd zrJSx4#fH|DZjf~{r_{I@u--ZK16#Y%cq}rmtwKqOk?4Aqrs16M#OnoX*EHv+-2q3!rjZ=UQolxvS=j2P&dMGl=Df7d@Br(CsA7M$QD?U z9-3~UjU*lkHORTVh3glWS#jur@ftlZUIF4UMi;+--5N?}|D~eC8{5M}ZveSkbWmg# z79JKGcc8#rT^oo90xw?5JQ^`qj_s1brHAHnr3GR@b!%}h7c4$C8C3KXzkonME$nA#7Wq2t2exd($M~k3%jD)qLv0`B{N%S7wpg!&4pgww zZ|;H}2)orQJ%sV@&i0PPe$-U35s8L$B_H%d*GPQMhG8Wp2ia1Hi z&T{c9kv&y|a_3R+8~;#9dE!5jk%|XGsR#Zik%Wih;O9uH@`2yW<;ed!ig^w%3oqBc z-`Y>3_@`p)F+4F?9YI@ef=5*ruwx7q86eYk4zR0j32G8AUORD`7ebn3VMl5!1qD}R z2)cpQcaA4Qp2)Xwi(YwEDin5(CP~WQgv|mgl`xmJtPX2Hb9z>bw&-2jVrAzdt419V zd1afT8Tl%bcaW@VSd0rRtw02WhP4gS;%X021S{BdNfrMJ1KQiI;Qu3o+^ErMck2HE DDsHmv literal 0 HcmV?d00001 diff --git a/body.html b/body.html new file mode 100644 index 0000000..1354ae2 --- /dev/null +++ b/body.html @@ -0,0 +1 @@ +< BLACK_MAN> yes, hello. black man here \ No newline at end of file diff --git a/readme.txt b/readme.txt new file mode 100644 index 0000000..21267c8 --- /dev/null +++ b/readme.txt @@ -0,0 +1,3 @@ +/technically/ standards-compliant http 1.1 server + +spec'd to RFC 2616 \ No newline at end of file diff --git a/response.py b/response.py new file mode 100644 index 0000000..c946b2a --- /dev/null +++ b/response.py @@ -0,0 +1,49 @@ +class status: + all_valid = { # https://www.w3.org/Protocols/rfc2616/rfc2616-sec6.html + 200: "OK",# add the rest l8r + 403: "Forbidden", + 404: "Not Found", + 500: "Internal Server Error", + 505: "HTTP Version not supported" + } + def __init__(self): + pass + @staticmethod + def add_status(status_line, code): + status_line.append(str(code)+" "+status.all_valid[code]) +class headers: + def __init__(self): + self.headers_dict = {} + def as_str(self): + ret = "" + for h in self.headers_dict: + ret += h+": "+str(self.headers_dict[h]) + ret += "\r\n" + return ret +class response: + def __init__(self): + self.status_line = [] + self.headers_obj = headers() + self.body = "" + def text(self): + return " ".join(self.status_line)+"\r\n"+self.headers_obj.as_str()+"\r\n"+self.body + @staticmethod + def build(query): + r = response() + + r.status_line.append("HTTP/1.1") + # check if query is heckin cute % valid/returnable + # always 200 for static testing + status.add_status(r.status_line, 200) + + f = open(query, "r") + r.body = f.read() + f.close() + + # manual for now + r.headers_obj.headers_dict["Server"] = "shttpile" + r.headers_obj.headers_dict["Content-Type"] = "text/html" + r.headers_obj.headers_dict["Content-Length"] = len(r.body.encode("ascii")) + r.headers_obj.headers_dict["X-Clacks-Overhead"] = "GNU Terry Pratchett, Aaron Swartz, Norm Macdonald" + + return r diff --git a/server.py b/server.py new file mode 100644 index 0000000..61177dd --- /dev/null +++ b/server.py @@ -0,0 +1,13 @@ +import socket +import response +s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) +s.bind(("0.0.0.0", 1337)) +s.listen() +while True: + c, c_add = s.accept() + print(c_add) + print(c.recv(1024).decode("ascii")) + r = response.response.build("body.html") + c.sendall(r.text().encode("ascii")) + c.close() +s.close()