-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweb_server.py
More file actions
124 lines (105 loc) · 3.74 KB
/
web_server.py
File metadata and controls
124 lines (105 loc) · 3.74 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
'''
Elaborato Programmazione di Reti
a.a. 2020/2021
Valerio Di Zio
Matricola: 942637
Traccia 2
Web Server in Python per un'azienda ospedaliera
'''
import sys
import signal
import http.server
import socketserver
import threading
import os
import base64
import zlib
# Path to the pdf file
FILEPATH = "pdf.pdf"
# Login credentials
USER = "admin"
PASSWORD = "programmazionedireti"
# IP adress
IP = '127.0.0.1'
# Value for the choice of compression
USE_GZIP_COMPRESSION = True
# Manage the wait, used for intercept CTRL+C
waiting_refresh = threading.Event()
# Accepting port number from standard input, else set default value
if sys.argv[1:]:
port = int(sys.argv[1])
else:
port = 8080
# Intercept CTRL+C
def signal_handler(signal, frame):
print(' Exiting http server (Ctrl+C pressed)')
try:
if(server):
server.server_close()
finally:
waiting_refresh.set()
sys.exit(0)
class ServerHandler(http.server.SimpleHTTPRequestHandler):
# Method of compressing using GZIP
@staticmethod
def gzip_encode(content):
gzip_compress = zlib.compressobj(9, zlib.DEFLATED, zlib.MAX_WBITS | 16)
data = gzip_compress.compress(content) + gzip_compress.flush()
return data
# Method for verifying credentials using base64
@staticmethod
def check_credentials(auth_header):
token = auth_header.split(" ")[1]
data = USER + ":" + PASSWORD
encoded_bytes = base64.b64encode(data.encode("utf-8"))
encoded_str = str(encoded_bytes)
return token == encoded_str
def do_GET(self):
auth_header = self.headers.get('Authorization')
if auth_header == None or not self.check_credentials(auth_header):
self.send_response(401)
self.send_header('WWW-Authenticate',
'Basic realm="Area Riservata"')
self.end_headers()
else:
if(self.path == "/"+FILEPATH):
with open(FILEPATH, 'rb') as f:
self.send_response(200)
self.send_header("Content-Type", 'application/pdf')
self.send_header(
"Content-Disposition", 'attachment; filename="{}"'.format(os.path.basename(FILEPATH)))
fs = os.fstat(f.fileno())
raw_content_length = fs.st_size
content = f.read()
if USE_GZIP_COMPRESSION:
print("Download the document compressed with GZIP")
self.send_header("Content-Encoding", "gzip")
content = self.gzip_encode(content)
compressed_content_length = len(content)
self.send_header("Content-Length",
compressed_content_length)
else:
print("Uncompressed document download")
self.send_header("Content-Length", raw_content_length)
self.end_headers()
self.wfile.write(content)
else:
http.server.SimpleHTTPRequestHandler.do_GET(self)
# ThreadingTCPServer allows many concurrent requests
server = socketserver.ThreadingTCPServer((IP, port), ServerHandler)
def main():
print("Running...")
# Indicates whether or not the server should wait for thread termination
server.daemon_threads = True
# Rebind existing port number
server.allow_reuse_address = True
# intercept CTRL+C
signal.signal(signal.SIGINT, signal_handler)
try:
while True:
server.serve_forever()
except KeyboardInterrupt:
pass
server.server_close()
if __name__ == "__main__":
main()