forked from DigNZ/mobilize.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testserver.py
96 lines (76 loc) · 2.86 KB
/
testserver.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
#!/usr/bin/python
"""
Test web server for mobilize.js
"""
import os
import SimpleHTTPServer
import SocketServer
import urllib
import Cookie
PORT = 8080
class Handler(SimpleHTTPServer.SimpleHTTPRequestHandler):
base = SimpleHTTPServer.SimpleHTTPRequestHandler
def send_head(self):
"""Common code for GET and HEAD commands.
This sends the response code and MIME headers.
Return value is either a file object (which has to be copied
to the outputfile by the caller unless the command was HEAD,
and must be closed by the caller under all circumstances), or
None, in which case the caller has nothing further to do.
"""
path = self.translate_path(self.path)
f = None
if os.path.isdir(path):
for index in "index.html", "index.htm":
index = os.path.join(path, index)
if os.path.exists(index):
path = index
break
else:
return self.list_directory(path)
ctype = self.guess_type(path)
if ctype.startswith('text/'):
mode = 'r'
else:
mode = 'rb'
try:
f = open(path, mode)
except IOError:
self.send_error(404, "File not found")
return None
self.send_response(200)
self.send_header("Content-type", ctype)
# Allow AJAXy from directly opened test files
self.send_header("Access-Control-Allow-Origin", "*")
self.end_headers()
return f
def do_GET(self):
#print "doGET", self.path
if "cookie" in self.headers.dict:
cookies = Cookie.SimpleCookie()
cookies.load(self.headers.dict["cookie"])
mobilize = cookies.get("mobilize-mobile",None)
if mobilize:
if mobilize.value == "1":
print "Cookie says client is mobile"
if "/log?" in self.path:
msg = urllib.unquote(self.path.split("msg=")[-1])
while msg.endswith("/"):
msg = msg[:-1]
print msg
self.send_response(200)
self.end_headers()
return
return self.base.do_GET(self)
def log_request(self,code):
if "/log" in self.path:
return
return self.base.log_request(self, code)
httpd = SocketServer.TCPServer(("", PORT), Handler)
httpd.allow_reuse_address = True
print "serving at port", PORT
# This is handy if your terminal supports double clicking of the linkss
for file in os.listdir(os.path.join(os.getcwd(), "tests")):
if file.endswith(".html"):
print "Open test page http://localhost:%d/tests/%s?mobilize=true" % (PORT, file)
httpd.serve_forever()