-
Notifications
You must be signed in to change notification settings - Fork 0
/
FileHostHandler.py
52 lines (49 loc) · 1.45 KB
/
FileHostHandler.py
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
#!/usr/bin/env python3
import node
import sys
import json
import http
from urllib.parse import urlparse, parse_qs
permFile = None
class host_handler(http.server.BaseHTTPRequestHandler):
def do_GET(self):
print("PATH:",str(self.path))
params = parse_qs(urlparse(self.path).query)
if not permFile and 'file' not in params:
self.send_response(400)
self.send_header('Content-type','text')
self.end_headers()
self.wfile.write("Please specify a file parameter".encode())
return
try:
#print("Loo(king for file:", self.headers.as_string())
fileName = permFile or params['file'][0]
if (fileName.endswith('.ico') or fileName.endswith('.png') or fileName.endswith('.jpg')):
content_type = 'image'
elif (fileName.endswith('.html')):
content_type = 'text/html'
else:
content_type = 'text'
with open(fileName, 'rb') as file:
self.send_response(200)
self.send_header('Content-type','text')
self.end_headers()
self.wfile.write(file.read())
except Exception as e:
print(str(e))
self.send_response(404)
self.send_header('Content-type','text')
self.end_headers()
self.wfile.write("File not found".encode())
return "Error: cannot find file".encode()
if __name__ == "__main__":
host = "localhost"
service = "/"
if len(sys.argv) >= 2:
host = sys.argv[1]
if len(sys.argv) >= 3:
service = sys.argv[2]
if len(sys.argv) >= 4:
global permFile
permFile = sys.argv[3]
node.runner(host_handler, host, service)