-
Notifications
You must be signed in to change notification settings - Fork 0
/
server_demo.py
138 lines (119 loc) · 4.59 KB
/
server_demo.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
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
import os
import time
import socket
from http.client import HTTPResponse
from datetime import datetime, timedelta
GMT_FORMAT = "%a, %d %b %Y %H:%M:%S\n"
HOST = "127.0.0.1"
PORT = 65432
FRESH_WHEN = 20
def main():
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind((HOST,PORT))
s.listen()
while True:
conn,addr = s.accept()
print("Connected by {}".format(addr))
request = conn.recv(4096).decode()
if not request:
break
# print(request)
# parse the request header from client
headers = request.split('\n')
dest = headers[0].split()[1].replace('\n', '').replace('\r', '')
# print("dest",dest)
# hostname
hostn = dest.split('/')[1]
# print("host",hostn)
#pathname
filename = dest[dest.find(hostn,1)+len(hostn):]
if filename == "":
filename = "/"
print("Requesting for {}".format(hostn+filename))
content = fetch_file(hostn,filename)
if content:
response = "HTTP/1.0 200 OK\n\n".encode()+content
else:
response = 'HTTP/1.0 404 NOT FOUND\n\n File Not Found'.encode()
conn.sendall(response)
print("\n")
def fetch_file(hostn,filename):
file_from_cache= fetch_from_cache(hostn+filename)
if file_from_cache:
last_time = os.path.getmtime("./cache/"+(hostn+filename).replace("/","_").replace("?","_"))
fileTime = time.localtime(last_time)
# conditional get
print("Time diff: {}".format(time.time() - last_time))
if time.time() - last_time>FRESH_WHEN:
flag = 1
print("File in cache is expired.Fetching from server...")
file_from_server,modifiedFlag= fetch_from_server(hostn,filename,flag,fileTime)
if file_from_server:
print("File is from server.")
save_in_cache(hostn+filename,file_from_server)
return file_from_server
elif not modifiedFlag:
print("File has not been modified, so file is from cache.")
return file_from_cache
else:
return None
else:
print("Fetch from cache successfully!")
return file_from_cache
else:
# the file is not in cache
print("Not in cache.Fetching from server...")
file_from_server= fetch_from_server(hostn,filename)
if file_from_server:
save_in_cache(hostn+filename,file_from_server)
return file_from_server
else:
return None
def fetch_from_cache(filename):
try:
tar = filename.replace(':','_').replace('/','_').replace("?","_")
file = open('./cache/' + tar,'rb')
# print(time)
content = file.read()
file.close()
return content
except IOError:
return None
def fetch_from_server(hostn,filename,conditionalGet = False,fileTime = 0):
url = filename
conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print(url)
try:
conn.connect((hostn,5678))
if conditionalGet:
# request = ("GET "+url+" HTTP/1.1\r\n"+"Host: "+hostn+"\r\n"+"If-Modified-Since:"+" Tue, 07 Jun 2022 21:39:31"+"\r\n"+"Accept:*/*\r\n"+"\r\n").encode()
request = ("GET "+url+" HTTP/1.1\r\n"+"Host: "+hostn+"\r\n"+"If-Modified-Since:"+time.strftime(GMT_FORMAT,fileTime).replace("\n","")+"\r\n"+"Accept:*/*\r\n"+"\r\n").encode()
else:
request = ("GET "+url+" HTTP/1.1\r\n"+"Host: "+hostn+"\r\n"+"Accept:*/*\r\n"+"\r\n").encode()
# response = conn.makefile('rb')
# print(request)
conn.send(request)
response = HTTPResponse(conn)
response.begin()
# print(response.getheaders())
print(response.status)
if response.status == 200:
return response.read(),True
elif response.status == 304:
return None,False
else:
return None,True
except Exception as e:
print(e)
return None,True
def save_in_cache(filename,content):
print('Saving a copy of {} in the cache'.format(filename))
tar = filename.replace(':','_').replace('/','_').replace("?","_")
cached_file = open('./cache/' + tar, 'wb')
# cached_file.write(time+'\n')
cached_file.write(content)
cached_file.close()
print('Saving suceed')
if __name__ == '__main__':
while True:
main()