From b616a4466ac9169616ef6eb050cba78ed4e8f0d9 Mon Sep 17 00:00:00 2001 From: mindchasers Date: Thu, 2 May 2019 14:12:04 -0400 Subject: initial commit --- httpsrv.py | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 httpsrv.py (limited to 'httpsrv.py') 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'') + self.wfile.write(b'') + self.wfile.write(b'
') + self.wfile.write(b'Enter something:\ + \ + ') + self.wfile.write(b'
') + self.wfile.write(b'') + + 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'') + self.wfile.write(b'') + self.wfile.write(b'

You submitted ' + bytes(value,'utf-8') + b'

') + self.wfile.write(b'') + +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) -- cgit v1.2.3-8-gadcc