3 from os
import curdir
, sep
4 from BaseHTTPServer
import BaseHTTPRequestHandler
, HTTPServer
5 import posixpath
, urllib
, os
9 class MyHandler(BaseHTTPRequestHandler
):
11 if self
.path
.endswith(".afu"):
16 self
.copyfile(f
, self
.wfile
)
25 path
= self
.translate_path(self
.path
)
26 if os
.path
.isdir(path
):
27 self
.send_error(403, "Directory listing not supported")
32 self
.send_error(404, "File not found")
34 self
.send_response(200)
35 self
.send_header("Content-type", self
.guess_type(path
))
39 def translate_path(self
, path
):
40 path
= posixpath
.normpath(urllib
.unquote(path
))
41 words
= string
.splitfields(path
, '/')
42 words
= filter(None, 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
)
51 def copyfile(self
, source
, outputfile
):
54 data
= source
.read(BLOCKSIZE
)
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
]
66 return self
.extensions_map
['']
69 '': 'text/plain', # Default, *must* be present
74 '.jpeg': 'image/jpeg',
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
):
84 elif self
.path
.endswith("hint.afu"):
86 elif self
.path
.endswith("menue.afu"):
88 elif self
.path
.endswith("askquestion.afu"):
90 elif self
.path
.endswith("showquestion.afu"):
91 self
.AskQuestion(update
=False)
96 self
.base
="http://127.0.0.1:8080"
97 self
.stylefile
=self
.base
+"/style.css"
99 self
.wfile
.write ("<html><head><base href="+self
.base
+"/Questions/>")
100 self
.wfile
.write ("<link href="+self
.stylefile
+" rel=stylesheet type=text/css>")
101 self
.wfile
.write ("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf8\">")
103 self
.wfile
.write ("<SCRIPT LANGUAGE=\"JavaScript\">")
104 self
.wfile
.write ("document.onkeydown = eval_key; ")
105 self
.wfile
.write ("function eval_key (e) {")
106 self
.wfile
.write ("var keycode; if (window.event) keycode = window.event.keyCode; else if (e) keycode = e.which;")
107 self
.wfile
.write ("if (keycode == 65) { window.alert(\"abc\");window.location = \""+self
.base
+"/a.afu\";")
108 self
.wfile
.write ("} else if (keycode == 67) { window.location = \""+self
.base
+"/b.afu\";")
109 self
.wfile
.write ("} else if (keycode == 68) { window.location = \""+self
.base
+"/c.afu\";")
110 self
.wfile
.write ("} else if (keycode == 69) { window.location = \""+self
.base
+"/d.afu\";")
111 self
.wfile
.write ("}")
112 self
.wfile
.write ("</script>")
114 self
.wfile
.write ("</head><body>")
116 def DisplayHint(self
):
119 self
.wfile
.write ("<html><head><meta http-equiv=refresh content=\"0; URL=/"+f
.hint
+"\"></head></html>")
121 def SendHeader(self
):
122 self
.send_response(200)
123 self
.send_header('Content-type', 'text/html')
126 def DisplayQuestion(self
):
127 self
.wfile
.write("<div class=id>"+f
.id+"</div>")
128 self
.wfile
.write("<div class=question>")
129 self
.wfile
.write(f
.question
.encode("utf8"))
130 self
.wfile
.write("</div>")
131 self
.wfile
.write("<div class=statistics>Richtig: "+f
.correct
+" <br>Falsch: "+f
.wrong
+"</div>")
133 def WrongAnswer(self
):
136 self
.wfile
.write("<div class=wronganswer>Falsche Antwort</div>")
137 self
.DisplayQuestion()
138 self
.wfile
.write("<div class=correctanswer>"+f
.answercorrect
.encode("utf8")+"</div>")
141 def StartDisplay(self
):
143 self
.base
="http://127.0.0.1:8080"
145 self
.wfile
.write("<frameset border=0 frameborder=0 framespacing=0 marginwidth=0 rows=30px,*>")
146 self
.wfile
.write("<frame name=menue src=menue.afu scrolling=no noresize>")
147 self
.wfile
.write("<frame name=main src=askquestion.afu scrolling=auto noresize>")
148 self
.wfile
.write("</frameset>")
150 def AskQuestion(self
,update
=True):
151 self
.base
="http://127.0.0.1:8080"
153 self
.wfile
.write("<body>")
158 self
.DisplayQuestion()
160 self
.wfile
.write("<div class=answer>")
161 self
.wfile
.write("<a href="+self
.base
+"/a.afu class=button1>A</a>"+f
.answera
.encode("utf8")+"<br>")
162 self
.wfile
.write("<a href="+self
.base
+"/b.afu class=button1>B</a>"+f
.answerb
.encode("utf8")+"<br>")
163 self
.wfile
.write("<a href="+self
.base
+"/c.afu class=button1>C</a>"+f
.answerc
.encode("utf8")+"<br>")
164 self
.wfile
.write("<a href="+self
.base
+"/d.afu class=button1>D</a>"+f
.answerd
.encode("utf8")+"<br>")
165 self
.wfile
.write("</div>")
167 self
.wfile
.write("<div class=button>")
168 self
.wfile
.write("<a href="+self
.base
+"/a.afu class=button>A</a>")
169 self
.wfile
.write("<a href="+self
.base
+"/b.afu class=button>B</a>")
170 self
.wfile
.write("<a href="+self
.base
+"/c.afu class=button>C</a>")
171 self
.wfile
.write("<a href="+self
.base
+"/d.afu class=button>D</a>")
172 self
.wfile
.write("</div>")
174 self
.wfile
.write("<div class=hint>")
175 self
.wfile
.write("<a href="+self
.base
+"/hint.afu target=hint>Hinweis</a></div>")
177 self
.wfile
.write("</body></html>")
179 def DisplayMenu(self
):
181 self
.base
="http://127.0.0.1:8080"
182 self
.stylefile
=self
.base
+"/style.css"
184 self
.wfile
.write("<html><head><base target=main><link href="+self
.stylefile
+" rel=stylesheet type=text/css></head>")
185 self
.wfile
.write("<body class=menue><div class=menue>")
186 self
.wfile
.write("<a class=menue href="+self
.base
+"/askquestion.afu>Abfragen</a>")
187 self
.wfile
.write("<a class=menue href="+self
.base
+"/showquestion.afu>Zurück zur Frage</a>")
188 self
.wfile
.write("<a class=menue href="+self
.base
+"/method.afu>Abfragemethode</a>")
189 self
.wfile
.write("<a class=menue href="+self
.base
+"/statistic.afu>Statistik</a>")
190 self
.wfile
.write("</div></body></html>")
195 ctype
, pdict
= cgi
.parse_header(self
.headers
.getheader('content-type'))
196 if ctype
== 'multipart/form-data':
197 query
=cgi
.parse_multipart(self
.rfile
, pdict
)
198 self
.send_response(301)
201 upfilecontent
= query
.get('upfile')
202 print "filecontent", upfilecontent
[0]
203 self
.wfile
.write("<HTML>POST OK.<BR><BR>");
204 self
.wfile
.write(upfilecontent
[0]);
209 f
= framework
.Framework()
210 server
= HTTPServer(('', 8080), MyHandler
)
211 print 'started httpserver...'
212 server
.serve_forever()
213 except KeyboardInterrupt:
214 print '^C received, shutting down server'
215 server
.socket
.close()
217 if __name__
== '__main__':