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
):
94 self
.send_response(200)
95 self
.send_header('Content-type', 'text/html')
99 self
.wfile
.write ("<html><head><meta http-equiv=refresh content=\"0; URL=/"+f
.hint
+"\"></head></html>")
101 def DisplayQuestion(self
):
102 self
.wfile
.write("<div class=id>"+f
.id+"</div>")
103 self
.wfile
.write("<div class=question>")
104 self
.wfile
.write(f
.question
)
105 self
.wfile
.write("</div>")
107 def WrongAnswer(self
):
108 self
.send_response(200)
109 self
.send_header('Content-type', 'text/html')
112 self
.base
="http://127.0.0.1:8080"
113 self
.wfile
.write("<html><head></head><body>")
115 self
.DisplayQuestion()
120 def StartDisplay(self
):
121 self
.send_response(200)
122 self
.send_header('Content-type', 'text/html')
125 self
.base
="http://127.0.0.1:8080"
127 self
.wfile
.write("<frameset border=0 frameborder=0 framespacing=0 marginwidth=0 rows=70px,*>")
128 self
.wfile
.write("<frame name=menue src=menue.afu scrolling=no noresize>")
129 self
.wfile
.write("<frame name=main src=askquestion.afu scrolling=auto noresize>")
130 self
.wfile
.write("</frameset>")
132 def AskQuestion(self
):
133 self
.base
="http://127.0.0.1:8080"
134 self
.wfile
.write("<html><head><base href="+self
.base
+"/Questions/></head><body>")
137 self
.DisplayQuestion()
139 self
.wfile
.write("<div class=answer>")
140 self
.wfile
.write("<a href="+self
.base
+"/a.afu class=button>A</a>"+f
.answera
+"<br>")
141 self
.wfile
.write("<a href="+self
.base
+"/b.afu class=button>B</a>"+f
.answerb
+"<br>")
142 self
.wfile
.write("<a href="+self
.base
+"/c.afu class=button>C</a>"+f
.answerc
+"<br>")
143 self
.wfile
.write("<a href="+self
.base
+"/d.afu class=button>D</a>"+f
.answerd
+"<br>")
144 self
.wfile
.write("</div>")
146 self
.wfile
.write("<div class=hint>")
147 self
.wfile
.write("<a href="+self
.base
+"/hint.afu>Hinweis</a></div>")
149 self
.wfile
.write("</body></html>")
151 def DisplayMenu(self
):
152 self
.send_response(200)
153 self
.send_header('Content-type', 'text/html')
156 self
.base
="http://127.0.0.1:8080"
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>")
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)
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]);
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__':