comparison pythonServer.py @ 1118:3edcbbea168b

Added schema for Test Specification Document for validation (wip)
author Nicholas Jillings <n.g.r.jillings@se14.qmul.ac.uk>
date Wed, 23 Dec 2015 12:41:08 +0000
parents
children eef2d4ea18fb
comparison
equal deleted inserted replaced
-1:000000000000 1118:3edcbbea168b
1 import BaseHTTPServer
2 from os import walk
3 from os import path
4 from os import listdir
5 import inspect
6 import os
7 import urllib2
8 import pickle
9 import datetime
10
11 # Go to right folder.
12 scriptdir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))) # script directory
13 os.chdir(scriptdir) # does this work?
14
15 PSEUDO_PATH = 'example_eval/'
16 pseudo_files = []
17 for filename in listdir(PSEUDO_PATH):
18 if filename.endswith('.xml'):
19 pseudo_files.append(filename)
20
21 curSaveIndex = 0;
22 curFileName = 'test-0.xml'
23 while(path.isfile('saves/'+curFileName)):
24 curSaveIndex += 1;
25 curFileName = 'test-'+str(curSaveIndex)+'.xml'
26
27 pseudo_index = curSaveIndex % len(pseudo_files)
28
29 print 'URL: http://localhost:8000/index.html'
30
31 def send404(s):
32 s.send_response(404)
33 s.send_header("Content-type", "text/html")
34 s.end_headers()
35
36 def processFile(s):
37 s.path = s.path.rsplit('?')
38 s.path = s.path[0]
39 s.path = s.path[1:len(s.path)]
40 st = s.path.rsplit(',')
41 lenSt = len(st)
42 fmt = st[lenSt-1].rsplit('.')
43 size = path.getsize(urllib2.unquote(s.path))
44 fileDump = open(urllib2.unquote(s.path))
45 s.send_response(200)
46
47 if (fmt[1] == 'html'):
48 s.send_header("Content-type", 'text/html')
49 elif (fmt[1] == 'css'):
50 s.send_header("Content-type", 'text/css')
51 elif (fmt[1] == 'js'):
52 s.send_header("Content-type", 'application/javascript')
53 else:
54 s.send_header("Content-type", 'application/octet-stream')
55 s.send_header("Content-Length", size)
56 s.end_headers()
57 s.wfile.write(fileDump.read())
58 fileDump.close()
59
60 def saveFile(self):
61 global curFileName
62 global curSaveIndex
63 varLen = int(self.headers['Content-Length'])
64 postVars = self.rfile.read(varLen)
65 print curFileName
66 file = open('saves/'+curFileName,'w')
67 file.write(postVars)
68 file.close()
69 try:
70 wbytes = os.path.getsize('saves/'+curFileName)
71 except OSError:
72 self.send_response(200)
73 self.send_header("Content-type", "text/xml")
74 self.end_headers()
75 self.wfile.write('<response state="error"><message>Could not open file</message></response>')
76 self.send_response(200)
77 self.send_header("Content-type", "text/xml")
78 self.end_headers()
79 self.wfile.write('<response state="OK"><message>OK</message><file bytes="'+str(wbytes)+'">"saves/'+curFileName+'"</file></response>')
80 curSaveIndex += 1
81 curFileName = 'test-'+str(curSaveIndex)+'.xml'
82
83 class MyHandler(BaseHTTPServer.BaseHTTPRequestHandler):
84 def do_HEAD(s):
85 s.send_response(200)
86 s.send_header("Content-type", "text/html")
87 s.end_headers()
88 def do_GET(request):
89 global pseudo_index
90 global pseudo_files
91 global PSEUDO_PATH
92 if(request.client_address[0] == "127.0.0.1"):
93 if (request.path == "/favicon.ico"):
94 send404(request)
95 else:
96 if (request.path == '/'):
97 request.path = '/index.html'
98 elif (request.path == '/pseudo.xml'):
99 request.path = '/'+PSEUDO_PATH + pseudo_files[pseudo_index]
100 print request.path
101 pseudo_index += 1
102 pseudo_index %= len(pseudo_files)
103 processFile(request)
104 else:
105 send404(request)
106
107 def do_POST(request):
108 if(request.client_address[0] == "127.0.0.1"):
109 if (request.path == "/save" or request.path == "/save.php"):
110 saveFile(request)
111 else:
112 send404(request)
113
114 def run(server_class=BaseHTTPServer.HTTPServer,
115 handler_class=MyHandler):
116 server_address = ('', 8000)
117 httpd = server_class(server_address, handler_class)
118 httpd.serve_forever()
119
120 run()