aboutsummaryrefslogtreecommitdiffstats
path: root/python/dotsandboxes/dotsandboxesserver.py
blob: 0a1ed87aaf0267e726413abf7ae02a51ef0d9fa7 (plain) (blame)
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
#!/usr/bin/env python3
# encoding: utf-8
"""
dotsandboxesserver.py

Template for the Machine Learning Project course at KU Leuven (2017-2018)
of Hendrik Blockeel and Wannes Meert.

Copyright (c) 2018 KU Leuven. All rights reserved.
"""

import sys
import argparse
import logging
import http.server
import socketserver
import json

logger = logging.getLogger(__name__)


class RequestHandler(http.server.SimpleHTTPRequestHandler):
    def do_GET(self):
        if self.path == "/":
            self.send_response(302)
            self.send_header(
                "Location", "python/dotsandboxes/static/dotsandboxes.html")
            self.end_headers()
        return super().do_GET()

    def do_PUT(self):
        response = {
            'result': 'ok'
        }
        self.send_response(200)
        self.send_header('Content-type', 'application/json')
        self.end_headers()
        self.wfile.write(json.dumps(response).encode())


def start_server(port):
    with socketserver.TCPServer(("", port), RequestHandler) as httpd:
        print("Running on http://127.0.0.1:{}".format(port))
        httpd.serve_forever()


def main(argv=None):
    parser = argparse.ArgumentParser(description='Start server to play Dots and Boxes')
    parser.add_argument('--verbose', '-v', action='count', default=0, help='Verbose output')
    parser.add_argument('--quiet', '-q', action='count', default=0, help='Quiet output')
    parser.add_argument('port', metavar='PORT', type=int, help='Port to use for server')
    args = parser.parse_args(argv)

    logger.setLevel(max(logging.INFO - 10 * (args.verbose - args.quiet), logging.DEBUG))
    logger.addHandler(logging.StreamHandler(sys.stdout))

    start_server(args.port)


if __name__ == "__main__":
    sys.exit(main())