#!/usr/bin/env python3
from http.server import BaseHTTPRequestHandler, HTTPServer
import re
class MyHandler(BaseHTTPRequestHandler):
def _set_response(self):
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
def do_GET(self):
self._set_response()
self.wfile.write(bytes("""
<form action="/" method="post">
Val1:<br>
<input type="text" name="val1" value="1">
<br>
Val2:<br>
<input type="text" name="val2" value="2">
<br><br>
<input type="submit" value="Submit">
</form>
""", "utf-8"))
def do_POST(self):
content_length = int(self.headers['Content-Length'])
post_data = self.rfile.read(content_length)
post_data = re.split("&", post_data.decode("utf-8"))
val1 = int(re.split("=", post_data[0])[1])
val2 = int(re.split("=", post_data[1])[1])
self._set_response()
self.wfile.write(bytes("%s" % (val1 + val2), "utf-8"))
def run(server_class=HTTPServer, handler_class=MyHandler, port=8080):
server_address = ('', port)
httpd = server_class(server_address, handler_class)
print('Starting httpd...')
httpd.serve_forever()
if __name__ == "__main__":
run()