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"):
93 def DisplayHint(self
):
96 self
.wfile
.write ("<html><head><meta http-equiv=refresh content=\"0; URL=/"+f
.hint
+"\"></head></html>")
99 self
.send_response(200)
100 self
.send_header('Content-type', 'text/html')
103 def DisplayQuestion(self
):
104 self
.wfile
.write("<div class=id>"+f
.id+"</div>")
105 self
.wfile
.write("<div class=question>")
106 self
.wfile
.write(f
.question
)
107 self
.wfile
.write("</div>")
109 def WrongAnswer(self
):
111 self
.base
="http://127.0.0.1:8080"
112 self
.wfile
.write("<html><head><base href="+self
.base
+"/Questions/></head><body>")
113 self
.wfile
.write("<div class=wronganswer>Falsche Antwort</div><br>")
114 self
.DisplayQuestion()
115 self
.wfile
.write("<div class=correctanswer>"+f
.answercorrect
+"</div>")
118 def StartDisplay(self
):
120 self
.base
="http://127.0.0.1:8080"
122 self
.wfile
.write("<frameset border=0 frameborder=0 framespacing=0 marginwidth=0 rows=70px,*>")
123 self
.wfile
.write("<frame name=menue src=menue.afu scrolling=no noresize>")
124 self
.wfile
.write("<frame name=main src=askquestion.afu scrolling=auto noresize>")
125 self
.wfile
.write("</frameset>")
127 def AskQuestion(self
):
128 self
.base
="http://127.0.0.1:8080"
129 self
.wfile
.write("<html><head><base href="+self
.base
+"/Questions/></head><body>")
132 self
.DisplayQuestion()
134 self
.wfile
.write("<div class=answer>")
135 self
.wfile
.write("<a href="+self
.base
+"/a.afu class=button>A</a>"+f
.answera
+"<br>")
136 self
.wfile
.write("<a href="+self
.base
+"/b.afu class=button>B</a>"+f
.answerb
+"<br>")
137 self
.wfile
.write("<a href="+self
.base
+"/c.afu class=button>C</a>"+f
.answerc
+"<br>")
138 self
.wfile
.write("<a href="+self
.base
+"/d.afu class=button>D</a>"+f
.answerd
+"<br>")
139 self
.wfile
.write("</div>")
141 self
.wfile
.write("<div class=hint>")
142 self
.wfile
.write("<a href="+self
.base
+"/hint.afu>Hinweis</a></div>")
144 self
.wfile
.write("</body></html>")
146 def DisplayMenu(self
):
148 self
.base
="http://127.0.0.1:8080"
151 self
.wfile
.write("<html><head><base target=main><link href="+self
.stylefile
+" rel=stylesheet type=text/css></head>")
152 self
.wfile
.write("<body><div class=menue>")
153 self
.wfile
.write("<a class=menue href="+self
.base
+"/askquestion.afu>Abfragen</a>")
154 self
.wfile
.write("</div></body></html>")
159 ctype
, pdict
= cgi
.parse_header(self
.headers
.getheader('content-type'))
160 if ctype
== 'multipart/form-data':
161 query
=cgi
.parse_multipart(self
.rfile
, pdict
)
162 self
.send_response(301)
165 upfilecontent
= query
.get('upfile')
166 print "filecontent", upfilecontent
[0]
167 self
.wfile
.write("<HTML>POST OK.<BR><BR>");
168 self
.wfile
.write(upfilecontent
[0]);
173 f
= framework
.Framework()
174 server
= HTTPServer(('', 8080), MyHandler
)
175 print 'started httpserver...'
176 server
.serve_forever()
177 except KeyboardInterrupt:
178 print '^C received, shutting down server'
179 server
.socket
.close()
181 if __name__
== '__main__':