-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttps.py
More file actions
66 lines (56 loc) · 1.48 KB
/
https.py
File metadata and controls
66 lines (56 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import socket
import threading
import datetime as dt
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(("0.0.0.0", 65430))
s.listen(5)
# * Routers
pyti = {
"/": "page.html",
"/favicon.ico": "image_proxy.webp"
}
# * Types for static
types = {
"html": "text/html",
"css": "text/css"
}
def send_page(conn, addr):
print(f"Connected: {addr}")
# * Connect holding
req = conn.recv(1024).decode()
line = req.split("\n")[0]
meth, path, ver = line.split()
status, body, ext = check_pyti(path)
# * Page sending
ctype = types.get(ext)
page = f"""
HTTP/1.1 {status}
Content-Type: {ctype}
Content-Length: {len(body)}
{body}
"""
conn.send(page.encode("utf-8"))
# * logging connection
with open("aces.log", "w") as log:
log.write(f"[{dt.datetime.now().strftime("%m/%d/%Y, %H:%M:%S")}] {req}")
log.close()
conn.close()
# * Function for check routers
def check_pyti(path):
if path in pyti:
status = "200 OK"
filename =pyti[path]
ext = filename.split(".")[1]
with open(f"static/{filename}", "r", encoding="utf-8") as f:
body = f.read()
else:
status = "404 Not Found"
with open(f"static/404.html", "r", encoding="utf-8") as f:
body = f.read()
ext = "html"
return status, body, ext
while True:
conn, addr = s.accept()
threading.Thread(target=send_page, args=(conn, addr)).start()
req = conn.recv(1024).decode()
print(req)