summaryrefslogtreecommitdiff
path: root/httpsrv.py
blob: 5e08f5277e5592cb1d4b1fc69f7b27ab26f19539 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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)

Highly Recommended Verilog Books