def setup(cls): #load all static files #you can control which file to be seen here sw_log("building resource ...") cls.root = os.path.join(os.getcwd(),"resource") for root,dirs,cls.res_list in os.walk(cls.root): pass if len(cls.res_list) == 0: sw_err_print("no resources found")
def finish_response(self): try: if not self.result_is_file() or not self.sendfile(): if isinstance(self.result, basestring): self.write(self.result) else: for data in self.result: self.write(data) self.finish_content() except IOError as e: if e.errno == errno.EPIPE: sw_log("Browser closed") finally: self.close()
def wsgi(self, environ, start_response): """The WSGI-interface.""" sw_log('REQUEST %s:%s'%('PATH_INFO',environ['PATH_INFO'])) try: path = self.get_path(environ) func = route_super(path) start_response(self.response_status, self.response_head) return func(environ) except (KeyboardInterrupt, SystemExit, MemoryError): raise except Exception: err = '<h1>Critical error while processing request: %s</h1>'\ % sw_html_escape(environ.get('PATH_INFO', '/')) environ['wsgi.errors'].write(err) headers = [('Content-Type', 'text/html; charset=UTF-8')] start_response('500 INTERNAL SERVER ERROR', headers, sys.exc_info()) return [sw_tob(err)]
def log_message(self, format, *args): sw_log("%s %s" %(self.address_string(),format%args))
@route("/") def route_index(environ): return sw_Resource.get_res_path("index.html") @route("/index") def route_welcome(environ): return sw_Resource.get_res_path("index.html") @route("/login") def route_login(environ): name = sw_Form.get(environ,"name") pwd = sw_Form.get(environ,"pass") if not name or name == "": return "please input name" return sw_Resource.get_res_path("login.html").replace("""<%name%>""",name) @route("default") def route_default(environ): return sw_Resource.get_res_path("error404.html") if __name__ == '__main__': #wellcome sw_log('simple_web a handmade website,enjoy the building by hand!') #get the args host = "0.0.0.0" if len(sys.argv) < 3 else sys.argv[1] port = "80" if len(sys.argv) < 3 else sys.argv[2] simple_web.run(_app='simple_web_app',_server='simple_web_server',_host=host,_port=port)