diff options
author | mindchasers <repos@mindchasers.com> | 2019-05-02 14:12:04 -0400 |
---|---|---|
committer | mindchasers <repos@mindchasers.com> | 2019-05-02 14:12:04 -0400 |
commit | b616a4466ac9169616ef6eb050cba78ed4e8f0d9 (patch) | |
tree | 2bffe92208f3e96fb902429bfbb98284fb2fab49 /httpsrv.py |
Diffstat (limited to 'httpsrv.py')
-rw-r--r-- | httpsrv.py | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/httpsrv.py b/httpsrv.py new file mode 100644 index 0000000..5e08f52 --- /dev/null +++ b/httpsrv.py @@ -0,0 +1,68 @@ +""" + httpsrv.py + + Copyright 2019 (C) Mind Chasers Inc, + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + +""" + +from http.server import HTTPServer, BaseHTTPRequestHandler +import urllib + +HOST_ADDRESS = "" +HOST_PORT = 8000 +VERSION = "0.1a" + +class RequestHandler(BaseHTTPRequestHandler): + """ Our custom, example request handler """ + + def send_response(self, code, message=None): + """ override to customize header """ + self.log_request(code) + self.send_response_only(code) + self.send_header('Server','python3 http.server Development Server') + self.send_header('Date', self.date_time_string()) + self.end_headers() + + def do_GET(self): + """ response for a GET request """ + self.send_response(200) + self.wfile.write(b'<head><style>p, button {font-size: 1em}</style></head>') + self.wfile.write(b'<body>') + self.wfile.write(b'<form method="POST" enctype="application/x-www-form-urlencoded">') + self.wfile.write(b'<span>Enter something:</span>\ + <input name="test"> \ + <button style="color:blue">Submit</button>') + self.wfile.write(b'</form>') + self.wfile.write(b'</body>') + + def do_POST(self): + """ response for a POST """ + content_length = int(self.headers['Content-Length']) + (input,value) = self.rfile.read(content_length).decode('utf-8').split('=') + value = urllib.parse.unquote_plus(value) + self.send_response(200) + self.wfile.write(b'<head><style>p, button {font-size: 1em}</style></head>') + self.wfile.write(b'<body>') + self.wfile.write(b'<p>You submitted ' + bytes(value,'utf-8') + b'</p>') + self.wfile.write(b'</body>') + +def run(server_class=HTTPServer, handler_class=BaseHTTPRequestHandler): + """ follows example shown on docs.python.org """ + server_address = (HOST_ADDRESS, HOST_PORT) + httpd = server_class(server_address, handler_class) + httpd.serve_forever() + +if __name__ == '__main__': + run(handler_class=RequestHandler) |