-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_server.py
More file actions
231 lines (195 loc) · 7.03 KB
/
test_server.py
File metadata and controls
231 lines (195 loc) · 7.03 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#!/usr/bin/python
import threading
import sys,os
from os import curdir, sep
from SocketServer import ThreadingMixIn
from BaseHTTPServer import BaseHTTPRequestHandler,HTTPServer
global STATIC_DIR
PORT_NUMBER = 8080
""" html Top and Bottom is used to print out the directory listing
when on the home page."""
def htmlTop():
top = """<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<meta name= "viewport" content = "width=device-width,intiail-scale=1.0"/>
<title> Cs560 Project </title>
</head>
<body>
<h2> Directory </h2>"""
return top
def htmlBot():
bot ="""
</body>
</html>"""
return bot
""" Get files in current directory i.e active directory
This uses STATIC_DIR to always keep the main directory known.
This is useful when changing to different directories. Get_dfiles
grabs all the files in the current directory ( except .files) and
puts them in between the htmlTop() and htmlBot()"""
def Get_dFiles(h):
if h == '/':
os.chdir(STATIC_DIR)
else:
os.chdir(STATIC_DIR + h)
file_list = []
files = htmlTop() + "<ol>"
for f in os.listdir(os.getcwd()):
if f[0] == '.':
continue
if f.endswith(".py"):
continue
files += " <li> " "<a href = " +f + ">" + f + "</a>" "</li>"
file_list.append(f)
files +=" </ol>"
files += htmlBot()
return files
""" Class to handle threads. The code is the same as my handle class"""
class ThreadHandler(BaseHTTPRequestHandler):
def _set_headers(self):
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
def do_HEAD(self):
self._set_headers()
def do_GET(self):
checker = 0
t = 0
cgi = 0
try:
#Check the file extension required and
#set the right mime type
sendReply = False
if self.path.endswith(".html"):
mimetype='text/html'
sendReply = True
checker = 1
elif self.path.endswith(".cgi"):
mimetype = 'text/html'
cgi = 1
cgi_file = self.path
sendReply = True
else:
mimetype='text/html'
files = Get_dFiles(self.path + '/')
sendReply = True
t = 1
checker = 0
if sendReply == True:
#Open the static file requested and send it
self._set_headers()
if t == 1:
self.wfile.write(files)
if checker == 1:
f = open(curdir + sep + self.path)
self.wfile.write(f.read())
f.close()
if cgi == 1:
rin,rout = os.popen2('.'+ cgi_file)
rin.close()
dataout = rout.read()
rout.close()
self.wfile.write(dataout)
return
except IOError:
self.send_error(404,'File Not Found: %s' % self.path)
class ThreadedHTTPServer(ThreadingMixIn, HTTPServer):
pass
""" Class to handle Single Threads"""
class myHandler(BaseHTTPRequestHandler):
"""Headers are set to automicatically send a 200 reponse to the client"""
def _set_headers(self):
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
def do_HEAD(self):
self._set_headers()
"""Gets call from the client
Only checks for cgi and html files to execute
List the directory when neither are present"""
def do_GET(self):
checker = 0
t = 0
cgi = 0
try:
#Check the file extension required and
#set the right mime type
sendReply = False
if self.path.endswith(".html"):
mimetype='text/html'
sendReply = True
checker = 1
elif self.path.endswith(".cgi"):
mimetype = 'text/html'
cgi = 1
cgi_file = self.path
sendReply = True
else:
mimetype='text/html'
files = Get_dFiles(self.path + '/')
sendReply = True
t = 1
if sendReply == True:
#Open the static file requested and send it
self.send_response(200)
self.send_header('Content-type',mimetype)
self.end_headers()
if t == 1:
self.wfile.write(files)
if checker == 1:
f = open(curdir + sep + self.path)
self.wfile.write(f.read())
f.close()
if cgi == 1:
rin,rout = os.popen2('.'+ cgi_file)
rin.close()
dataout = rout.read()
rout.close()
self.wfile.write(dataout)
return
except IOError:
self.send_error(404,'File Not Found: %s' % self.path)
#Handler for the POST requests
def do_POST(self):
if self.path=="/send":
form = cgi.FieldStorage(
fp=self.rfile,
headers=self.headers,
environ={'REQUEST_METHOD':'POST',
'CONTENT_TYPE':self.headers['Content-Type'],
})
print "Your name is: %s" % form["your_name"].value
self.send_response(200)
self.end_headers()
self.wfile.write("Thanks %s !" % form["your_name"].value)
return
"""
Basic if statements to check if the server is single or multi threaded
If no argument is given. A Single threaded server is defaulted.
In order to exit out of the Server you need to CTRL-C"""
try:
#Create a web server and define the handler to manage the
#incoming request
STATIC_DIR = os.getcwd()
if len(sys.argv) == 1 or sys.argv[1] == 's':
server = HTTPServer(('', PORT_NUMBER), myHandler)
print 'Single Server Started httpserver on port ' , PORT_NUMBER
#Wait forever for incoming htto requests
server.serve_forever()
elif len(sys.argv) == 2 and sys.argv[1] == 'm':
server = ThreadedHTTPServer(('', PORT_NUMBER), ThreadHandler)
#threads = []
#for i in range(5):
server_thread = threading.Thread(target=server.serve_forever)
threads.append(server_thread)
server_thread.daemon = True
server_thread.start()
print("Server loop running in thread:", server_thread.name)
while True:
pass
server.shutdown()
except KeyboardInterrupt:
print '^C received, shutting down the web server'
server.server_close()