aboutsummaryrefslogtreecommitdiffhomepage
path: root/webserver/httpserver.py
blob: 2daae317ddbc61d133569140994857d5a3246202 (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
# Python 2.7 simple web server that serves a directory that contains a built Vue.js app
# Run this script from the command line with:


from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
import os
import time

class MyHandler(BaseHTTPRequestHandler):
    def do_GET(self):
        if self.path == '/dist':
            self.send_response(200)
            self.send_header('Content-type', 'text/html')
            self.end_headers()
            with open('dist/index.html', 'r') as f:
                self.wfile.write(f.read())
            return
        elif self.path == '/time':
            self.send_response(200)
            self.send_header('Content-type', 'text/html')
            self.end_headers()
            self.wfile.write(time.asctime())
            return
        # Host the /media/p4/Presets/setlists directory
        elif self.path == '/media/p4/Presets/setlists':
            self.send_response(200)
            self.send_header('Content-type', 'text/html')
            self.end_headers()
            with open('/media/p4/Presets/setlists', 'r') as f:
                self.wfile.write(f.read())
            return

        else:
            path = "." + self.path
            # Serve the file requested
            # Check if the file exists
            if os.path.isfile(path):
                self.send_response(200)
                self.send_header('Content-type', 'text/html')
                self.end_headers()
                with open(path, 'r') as f:
                    self.wfile.write(f.read())
            else:
                self.send_response(404)
                self.send_header('Content-type', 'text/html')
                self.end_headers()
                self.wfile.write('404: File not found')

            return


if __name__ == '__main__':
    try:
        server = HTTPServer(('0.0.0.0', 5555), MyHandler)
        print('Started httpserver on port ' , 5555)

        server.serve_forever()

    except KeyboardInterrupt:
        print('^C received, shutting down the web server')
        server.socket.close()