-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimple-http-server.py
More file actions
52 lines (44 loc) · 1.46 KB
/
simple-http-server.py
File metadata and controls
52 lines (44 loc) · 1.46 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
#!/usr/bin/python
# Based from https://www.acmesystems.it/python_http
from BaseHTTPServer import BaseHTTPRequestHandler,HTTPServer
import json
import os
WEB_APP_SERVER_PORT = int(os.environ.get('WEB_APP_SERVER_PORT', '8080'))
#This class will handles any incoming request from
#the browser
class myHandler(BaseHTTPRequestHandler):
#Handler for the GET requests
def do_GET(self):
try:
sendReply = False
if self.path.endswith("/"):
sendReply = True
if self.path.endswith("/mobile"):
sendReply = True
if self.path.endswith("/desktop"):
sendReply = True
if self.path.endswith("/laptop"):
sendReply = True
if self.path.endswith("netbook"):
sendReply = True
if sendReply == True:
mimetype='application/json'
json_string = json.dumps({'path': self.path})
#Open the static file requested and send it
self.send_response(200)
self.send_header('Content-type',mimetype)
self.end_headers()
self.wfile.write(json_string)
return
except IOError:
self.send_error(404,'File Not Found: %s' % self.path)
try:
#Create a web server and define the handler to manage the
#incoming request
server = HTTPServer(('', WEB_APP_SERVER_PORT), myHandler)
print 'Started httpserver on port' , WEB_APP_SERVER_PORT
#Wait forever for incoming htto requests
server.serve_forever()
except KeyboardInterrupt:
print '^C received, shutting down the web server'
server.socket.close()