|
|
@ -201,20 +201,18 @@ class WebSrv: |
|
|
|
""" |
|
|
|
""" |
|
|
|
Very basic web server. |
|
|
|
Very basic web server. |
|
|
|
""" |
|
|
|
""" |
|
|
|
def __init__(self, app_class, listen_addr='', listen_port=80): |
|
|
|
def __init__(self, request_handler, listen_addr='', listen_port=80): |
|
|
|
WebAppHandler.app_class = app_class |
|
|
|
|
|
|
|
httpd = BaseHTTPServer.HTTPServer((listen_addr, listen_port), |
|
|
|
httpd = BaseHTTPServer.HTTPServer((listen_addr, listen_port), |
|
|
|
WebAppHandler) |
|
|
|
request_handler) |
|
|
|
httpd.serve_forever() |
|
|
|
httpd.serve_forever() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class WebAppHandler(BaseHTTPRequestHandler): |
|
|
|
class WebAppHandler(BaseHTTPRequestHandler): |
|
|
|
""" |
|
|
|
""" |
|
|
|
Basic web server request handler. Handles GET and POST requests. Uses |
|
|
|
Basic web server request handler. Handles GET and POST requests. This class |
|
|
|
self.app_class (set by WebSrv) to dispatch GET and POST requests to a |
|
|
|
should be extended with methods (starting with 'h_') to handle the actual |
|
|
|
custom class. It looks for a method with the name of the request path in |
|
|
|
requests. If no path is set, it dispatches to the 'index' or 'default' |
|
|
|
self.app_class and then dispatches the request to that method. If no path |
|
|
|
method. |
|
|
|
is set, it dispatches to the 'index' or 'default' method. |
|
|
|
|
|
|
|
""" |
|
|
|
""" |
|
|
|
def do_GET(self): |
|
|
|
def do_GET(self): |
|
|
|
self.call(*self.parse(self.path)) |
|
|
|
self.call(*self.parse(self.path)) |
|
|
@ -241,14 +239,15 @@ class WebAppHandler(BaseHTTPRequestHandler): |
|
|
|
Find a method to call on self.app_class based on `path` and call it. |
|
|
|
Find a method to call on self.app_class based on `path` and call it. |
|
|
|
""" |
|
|
|
""" |
|
|
|
response_code = 200 |
|
|
|
response_code = 200 |
|
|
|
|
|
|
|
method_name = 'h_{}'.format(path) |
|
|
|
try: |
|
|
|
try: |
|
|
|
if hasattr(self.app_class, path) and \ |
|
|
|
if hasattr(self, method_name) and \ |
|
|
|
callable(getattr(self.app_class, path)): |
|
|
|
callable(getattr(self, method_name)): |
|
|
|
out = getattr(self.app_class, path)(**params) |
|
|
|
out = getattr(self, method_name)(**params) |
|
|
|
elif path == '' and hasattr(self.app_class, 'index'): |
|
|
|
elif path == '' and hasattr(self, 'index'): |
|
|
|
out = self.app_class.index(**params) |
|
|
|
out = self.index(**params) |
|
|
|
elif hasattr(self.app_class, 'default'): |
|
|
|
elif hasattr(self, 'default'): |
|
|
|
out = self.app_class.default(**params) |
|
|
|
out = self.default(**params) |
|
|
|
else: |
|
|
|
else: |
|
|
|
response_code = 404 |
|
|
|
response_code = 404 |
|
|
|
out = 'Not Found' |
|
|
|
out = 'Not Found' |
|
|
@ -261,18 +260,14 @@ class WebAppHandler(BaseHTTPRequestHandler): |
|
|
|
self.wfile.write(out) |
|
|
|
self.wfile.write(out) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ScriptFormWebApp: |
|
|
|
class ScriptFormWebApp(WebAppHandler): |
|
|
|
""" |
|
|
|
""" |
|
|
|
This class is a request handler for WebSrv. |
|
|
|
This class is a request handler for WebSrv. |
|
|
|
""" |
|
|
|
""" |
|
|
|
def __init__(self, vitaform, callbacks): |
|
|
|
|
|
|
|
self.vitaform = vitaform |
|
|
|
|
|
|
|
self.callbacks = callbacks |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def index(self): |
|
|
|
def index(self): |
|
|
|
return self.list() |
|
|
|
return self.h_list() |
|
|
|
|
|
|
|
|
|
|
|
def list(self): |
|
|
|
def h_list(self): |
|
|
|
h_form_list = [] |
|
|
|
h_form_list = [] |
|
|
|
for form_name, form_def in self.vitaform.forms.items(): |
|
|
|
for form_name, form_def in self.vitaform.forms.items(): |
|
|
|
h_form_list.append(''' |
|
|
|
h_form_list.append(''' |
|
|
@ -298,7 +293,7 @@ class ScriptFormWebApp: |
|
|
|
footer=html_footer, |
|
|
|
footer=html_footer, |
|
|
|
form_list=''.join(h_form_list)) |
|
|
|
form_list=''.join(h_form_list)) |
|
|
|
|
|
|
|
|
|
|
|
def form(self, form_name): |
|
|
|
def h_form(self, form_name): |
|
|
|
field_tpl = { |
|
|
|
field_tpl = { |
|
|
|
"string": '<input {} type="text" name="{}" />', |
|
|
|
"string": '<input {} type="text" name="{}" />', |
|
|
|
"number": '<input {} type="number" min="{}" max="{}" name="{}" />', |
|
|
|
"number": '<input {} type="number" min="{}" max="{}" name="{}" />', |
|
|
@ -387,7 +382,7 @@ class ScriptFormWebApp: |
|
|
|
submit_title=form.submit_title |
|
|
|
submit_title=form.submit_title |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
def submit(self, form_values): |
|
|
|
def h_submit(self, form_values): |
|
|
|
form_name = form_values.getfirst('form_name', None) |
|
|
|
form_name = form_values.getfirst('form_name', None) |
|
|
|
|
|
|
|
|
|
|
|
# Validate the form values |
|
|
|
# Validate the form values |
|
|
@ -481,9 +476,10 @@ class ScriptForm: |
|
|
|
return self.forms[form_name] |
|
|
|
return self.forms[form_name] |
|
|
|
|
|
|
|
|
|
|
|
def run(self, listen_addr='0.0.0.0', listen_port=80): |
|
|
|
def run(self, listen_addr='0.0.0.0', listen_port=80): |
|
|
|
vitaform = self |
|
|
|
ScriptFormWebApp.vitaform = self |
|
|
|
webapp = ScriptFormWebApp(vitaform, self.callbacks) |
|
|
|
ScriptFormWebApp.callbacks = self.callbacks |
|
|
|
WebSrv(webapp, listen_addr=listen_addr, listen_port=listen_port) |
|
|
|
#webapp = ScriptFormWebApp(vitaform, self.callbacks) |
|
|
|
|
|
|
|
WebSrv(ScriptFormWebApp, listen_addr=listen_addr, listen_port=listen_port) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
|
|
if __name__ == "__main__": |
|
|
|