.
[HamExam.git] / interface.py
blob4e3033988e6ca5bcfc6fb68c1846d2bfc8be55e2
1 #!/usr/bin/python
2 import string,cgi,time
3 from os import curdir, sep
4 from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
5 import posixpath, urllib, os
7 import framework
9 class MyHandler(BaseHTTPRequestHandler):
10 def do_GET(self):
11 if self.path.endswith(".afu"):
12 self.AFU()
13 else:
14 f = self.send_head()
15 if f:
16 self.copyfile(f, self.wfile)
17 f.close()
19 def do_HEAD(self):
20 f = self.send_head()
21 if f:
22 f.close()
24 def send_head(self):
25 path = self.translate_path(self.path)
26 if os.path.isdir(path):
27 self.send_error(403, "Directory listing not supported")
28 return None
29 try:
30 f = open(path, 'rb')
31 except IOError:
32 self.send_error(404, "File not found")
33 return None
34 self.send_response(200)
35 self.send_header("Content-type", self.guess_type(path))
36 self.end_headers()
37 return f
39 def translate_path(self, path):
40 path = posixpath.normpath(urllib.unquote(path))
41 words = string.splitfields(path, '/')
42 words = filter(None, words)
43 path = os.getcwd()
44 for word in words:
45 drive, word = os.path.splitdrive(word)
46 head, word = os.path.split(word)
47 if word in (os.curdir, os.pardir): continue
48 path = os.path.join(path, word)
49 return path
51 def copyfile(self, source, outputfile):
52 BLOCKSIZE = 8192
53 while 1:
54 data = source.read(BLOCKSIZE)
55 if not data: break
56 outputfile.write(data)
58 def guess_type(self, path):
59 base, ext = posixpath.splitext(path)
60 if self.extensions_map.has_key(ext):
61 return self.extensions_map[ext]
62 ext = string.lower(ext)
63 if self.extensions_map.has_key(ext):
64 return self.extensions_map[ext]
65 else:
66 return self.extensions_map['']
68 extensions_map = {
69 '': 'text/plain', # Default, *must* be present
70 '.html': 'text/html',
71 '.htm': 'text/html',
72 '.gif': 'image/gif',
73 '.jpg': 'image/jpeg',
74 '.jpeg': 'image/jpeg',
77 def AFU(self):
78 if self.path.endswith("a.afu") or self.path.endswith("b.afu") or self.path.endswith("c.afu") or self.path.endswith("d.afu"):
79 answer = (self.path.split("/")[-1]).replace(".afu","")
80 if not f.EvalQuestion (answer):
81 self.WrongAnswer()
82 else:
83 self.AskQuestion()
84 elif self.path.endswith("hint.afu"):
85 self.DisplayHint()
86 elif self.path.endswith("menue.afu"):
87 self.DisplayMenu()
88 elif self.path.endswith("askquestion.afu"):
89 self.AskQuestion()
90 else:
91 self.StartDisplay()
93 def ShowHead(self):
94 self.base="http://127.0.0.1:8080"
96 self.wfile.write ("<html><head><base href="+self.base+"/Questions/>")
97 self.wfile.write ("<link href="+self.stylefile+" rel=stylesheet type=text/css>")
98 self.wfile.write ("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf8\">")
99 self.wfile.write ("</head><body>")
101 def DisplayHint(self):
102 self.SendHeader()
103 print "Hint:",f.hint
104 self.wfile.write ("<html><head><meta http-equiv=refresh content=\"0; URL=/"+f.hint+"\"></head></html>")
106 def SendHeader(self):
107 self.send_response(200)
108 self.send_header('Content-type', 'text/html')
109 self.end_headers()
111 def DisplayQuestion(self):
112 self.wfile.write("<div class=id>"+f.id+"</div>")
113 self.wfile.write("<div class=question>")
114 self.wfile.write(f.question.encode("utf8"))
115 self.wfile.write("</div>")
117 def WrongAnswer(self):
118 self.SendHeader()
119 self.base="http://127.0.0.1:8080"
120 self.wfile.write("<html><head><base href="+self.base+"/Questions/></head><body>")
121 self.wfile.write("<div class=wronganswer>Falsche Antwort</div><br>")
122 self.DisplayQuestion()
123 self.wfile.write("<div class=correctanswer>"+f.answercorrect+"</div>")
126 def StartDisplay(self):
127 self.SendHeader()
128 self.base="http://127.0.0.1:8080"
130 self.wfile.write("<frameset border=0 frameborder=0 framespacing=0 marginwidth=0 rows=70px,*>")
131 self.wfile.write("<frame name=menue src=menue.afu scrolling=no noresize>")
132 self.wfile.write("<frame name=main src=askquestion.afu scrolling=auto noresize>")
133 self.wfile.write("</frameset>")
135 def AskQuestion(self):
136 self.base="http://127.0.0.1:8080"
137 self.wfile.write("<html><head><base href="+self.base+"/Questions/></head><body>")
139 f.AskQuestion()
140 self.DisplayQuestion()
142 self.wfile.write("<div class=answer>")
143 self.wfile.write("<a href="+self.base+"/a.afu class=button>A</a>"+f.answera+"<br>")
144 self.wfile.write("<a href="+self.base+"/b.afu class=button>B</a>"+f.answerb+"<br>")
145 self.wfile.write("<a href="+self.base+"/c.afu class=button>C</a>"+f.answerc+"<br>")
146 self.wfile.write("<a href="+self.base+"/d.afu class=button>D</a>"+f.answerd+"<br>")
147 self.wfile.write("</div>")
149 self.wfile.write("<div class=hint>")
150 self.wfile.write("<a href="+self.base+"/hint.afu>Hinweis</a></div>")
152 self.wfile.write("</body></html>")
154 def DisplayMenu(self):
155 self.SendHeader()
156 self.base="http://127.0.0.1:8080"
157 self.stylefile=""
159 self.wfile.write("<html><head><base target=main><link href="+self.stylefile+" rel=stylesheet type=text/css></head>")
160 self.wfile.write("<body><div class=menue>")
161 self.wfile.write("<a class=menue href="+self.base+"/askquestion.afu>Abfragen</a>")
162 self.wfile.write("</div></body></html>")
165 def do_POST(self):
166 global rootnode
167 ctype, pdict = cgi.parse_header(self.headers.getheader('content-type'))
168 if ctype == 'multipart/form-data':
169 query=cgi.parse_multipart(self.rfile, pdict)
170 self.send_response(301)
172 self.end_headers()
173 upfilecontent = query.get('upfile')
174 print "filecontent", upfilecontent[0]
175 self.wfile.write("<HTML>POST OK.<BR><BR>");
176 self.wfile.write(upfilecontent[0]);
178 def main():
179 try:
180 global f
181 f = framework.Framework()
182 server = HTTPServer(('', 8080), MyHandler)
183 print 'started httpserver...'
184 server.serve_forever()
185 except KeyboardInterrupt:
186 print '^C received, shutting down server'
187 server.socket.close()
189 if __name__ == '__main__':
190 main()