-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
157 lines (131 loc) · 5.13 KB
/
Copy pathserver.py
File metadata and controls
157 lines (131 loc) · 5.13 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import argparse, os, mimetypes
from socket import *
PORT = 9000
HOST = "localhost"
W_DIR = os.getcwd()
FILE = "index.html"
"""
example response:
['GET / HTTP/1.1', 'Host: localhost:9000', 'User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:135.0) Gecko/20100101 Firefox/135.0', 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'Accept-Language: en-US,en;q=0.5', 'Accept-Encoding: gzip, deflate, br, zstd', 'Connection: keep-alive', 'Upgrade-Insecure-Requests: 1', 'Sec-Fetch-Dest: document', 'Sec-Fetch-Mode: navigate', 'Sec-Fetch-Site: none', 'Sec-Fetch-User: ?1', 'Priority: u=0, i', '', '']
['GET /img HTTP/1.1', 'Host: localhost:9000', 'User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:135.0) Gecko/20100101 Firefox/135.0', 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'Accept-Language: en-US,en;q=0.5', 'Accept-Encoding: gzip, deflate, br, zstd', 'Connection: keep-alive', 'Upgrade-Insecure-Requests: 1', 'Sec-Fetch-Dest: document', 'Sec-Fetch-Mode: navigate', 'Sec-Fetch-Site: none', 'Sec-Fetch-User: ?1', 'Priority: u=0, i', '', '']
"""
def parseResponse(response: list[str]):
header = response[0].split(" ")
path = header[1]
method = header[0]
return (method, path)
def validateArgs(args):
if not os.path.isdir(args.dir):
raise Exception("Invalid directory")
if not os.path.isfile(os.path.join(args.dir, args.file)):
raise Exception("Invalid file")
def getCommandLineArguments():
parser = argparse.ArgumentParser()
parser.add_argument(
"-p",
"--port",
type=int,
default=PORT,
help=f"Port to listen on, default is {PORT}",
)
parser.add_argument(
"-addr",
"--address",
type=str,
default=HOST,
help=f"Address to listen on, default is {HOST}",
)
parser.add_argument(
"-d",
"--dir",
type=str,
default=W_DIR,
help=f"Working directory to serve from, default is current directory",
)
parser.add_argument("-v", "--verbose", action="store_true", help="Verbose mode")
parser.add_argument(
"-f",
"--file",
type=str,
default=f"{FILE}",
help=f"File to serve, default is index.html",
)
return parser.parse_args()
def router(url_path, working_dir, file):
url_path = url_path[1:]
if not os.path.exists(os.path.join(working_dir, url_path)):
raise Exception("File not found")
if url_path == "":
return os.path.join(working_dir, file)
else:
return os.path.join(working_dir, url_path)
def getHeaders(file_path, verbose):
mime = mimetypes.guess_type(file_path)[0]
if verbose:
print("mime -> ", mime)
return [
b"HTTP/1.1 200 OK\r\n",
b"Content-Type: " + mime.encode() + b"\r\n",
b"\r\n",
]
def createServer(args):
server_socket = socket(AF_INET, SOCK_STREAM)
try:
server_socket.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
server_socket.bind((args.address, args.port))
server_socket.listen(5)
while 1:
(client_socket, _) = server_socket.accept()
res = client_socket.recv(5000).decode()
res_arr = res.split("\r\n")
method, path = parseResponse(res_arr)
try:
if method == "GET":
file_path = router(path, args.dir, args.file)
if args.verbose:
print("path -> ", file_path)
header = getHeaders(file_path, args.verbose)
if args.verbose:
print("header -> ", header)
with open(file_path, "rb") as ff:
l = os.path.getsize(file_path)
data = b"".join(header)
data += ff.read(l)
client_socket.sendall(data)
client_socket.shutdown(SHUT_WR)
else:
response = b"".join(
[
b"HTTP/1.1 404 OK\r\n",
b"Content-Type: text/plain\r\n",
b"\r\n",
b"method not supported",
]
)
client_socket.sendall(response)
client_socket.shutdown(SHUT_WR)
except Exception as exc:
print("Error 1 -> ", exc)
response = b"".join(
[
b"HTTP/1.1 404 OK\r\n",
b"Content-Type: text/plain\r\n",
b"\r\n",
b"File not found",
]
)
client_socket.sendall(response)
client_socket.shutdown(SHUT_WR)
except KeyboardInterrupt:
print("Shutting down...")
except Exception as exc:
print("Error 2 -> ", exc)
server_socket.close()
args = getCommandLineArguments()
try:
validateArgs(args)
except Exception as exc:
print("Error 3 -> ", exc)
exit(1)
print(f"Listening on http://{args.address}:{args.port}")
createServer(args)