-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathajaxHandler.py
More file actions
85 lines (70 loc) · 2.3 KB
/
ajaxHandler.py
File metadata and controls
85 lines (70 loc) · 2.3 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
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
import threading
import webbrowser
import BaseHTTPServer
import SimpleHTTPServer
import json
from os import curdir, sep
PORT = 8080
appendix = {}
class ComplexEncoder(json.JSONEncoder):
def default(self, obj):
if hasattr(obj, 'isoformat'):
return obj.isoformat()
return json.JSONEncoder.default(self, obj)
class AjaxHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
print """The ajax handler is running..."""
def __init__(self, request, client_address, server):
SimpleHTTPServer.SimpleHTTPRequestHandler.__init__(self, request, client_address, server)
#Handler for the GET requests
def do_GET(self):
if self.path=="/":
self.path="/eyetracking.html"
try:
#Check the file extension required and
#set the right mime type
sendStaticFileReply = False
if self.path.endswith(".html"):
mimetype='text/html'
sendStaticFileReply = True
elif self.path.endswith(".jpg"):
mimetype='image/jpeg'
sendStaticFileReply = True
elif self.path.endswith(".gif"):
mimetype='image/gif'
sendStaticFileReply = True
elif self.path.endswith(".js"):
mimetype='application/javascript'
sendStaticFileReply = True
elif self.path.endswith(".json"):
mimetype='application/json'
sendStaticFileReply = True
elif self.path.endswith(".css"):
mimetype='text/css'
sendStaticFileReply = True
elif self.path.endswith(".png"):
mimetype='image/png'
sendStaticFileReply = True
elif self.path.endswith(".csv"):
mimetype='text/comma-separated-values'
sendStaticFileReply = True
if sendStaticFileReply == True:
#Open the static file requested and send it
f = open(curdir + sep + self.path, 'rb')
self.send_response(200)
self.send_header('Content-type',mimetype)
self.end_headers()
self.wfile.write(f.read())
f.close()
return
except IOError:
self.send_error(404,'File Not Found: %s' % self.path)
def start_server():
"""Start the server."""
server_address = ("", PORT)
server = BaseHTTPServer.HTTPServer(server_address, AjaxHandler)
server.serve_forever()
if __name__ == "__main__":
start_server()