import pyserv, sys # Adding response headers to this list will put them in any response that doesn't need specific headers. DEFAULT_RESPONSE_HEADERS = dict(Server=pyserv.VERSION) ending_mimes = {} ending_mimes["html"] = "text/html" ending_mimes["css"] = "text/css" ending_mimes["png"] = "image/png" class TestServ(pyserv.HTTPServer): def __init__(self): # Initialize base class. super().__init__() # Register GET and POST methods. self.register("GET",self.do_GET) self.register("POST",self.do_POST) # Add registries for other reasons self.contents = dict() self.mime = dict() self.cgi = dict() self.post = dict() # Add index.html content to be statically served. # with open("mcus.html") as f: # self.registerPage("/",[l.rstrip() for l in f],"text/html") # self.registerCGI("/",lambda x,y: [m.replace("PATH",x) for m in self.contents["/"]]) def registerPage(self,path,content,mime="text/plain"): self.contents[path] = content self.mime[path] = mime def registerCGI(self,path,func): self.cgi[path]=func def registerPost(self,path,func): self.post[path] = func # def greetingform(self,path,headers,data): # with open("greetings.html") as f: # return 200, DEFAULT_RESPONSE_HEADERS, [l.rstrip().format(pyserv.lazySan(data['name'][0] if data.get('name') else "stranger")) for l in f] def do_GET(self,method,path,headers): resp_headers = dict(Server=pyserv.VERSION) # with open("mcus.html") as f: # contents = [l.rstrip() for l in f] print("GET {}".format(path),file=sys.stderr) path = path if path!="/" else "index.html" try: with open("mcus/{}".format(path)) as f: contents = [f.read()] if path.split(".")[-1] in ending_mimes: resp_headers["Content-Type"] = ending_mimes[path.split(".")[-1]] except FileNotFoundError: return pyserv.abort(404) return self.formatResponse(200,resp_headers,contents) # Send contents along. def do_POST(self,m,path,headers,data): if path in self.post: try: # CGI functions return response code, response headers, and contents. respcode, resp_headers, contents = self.post[path](path,headers,pyserv.unquote(data)) # This data is directly passed into formatResponse to get a formatted response. return self.formatResponse(respcode,resp_headers,contents) except pyserv.Abort404: # Abort404 error is a signal return pyserv.abort(404) except: # If something else goes wrong, it's an internal error. return pyserv.abort(500) return pyserv.abort(405) # If path isn't in self.post, we aren't going to bother asking GET for it.