Server

mail@pastecode.io avatar
unknown
python
3 years ago
1.3 kB
3
Indexable
Never
from http.server import BaseHTTPRequestHandler, HTTPServer
import time
import _thread
import json
hostName = "localhost"
serverPort = 9000

maze = [
    ["B"] * 5,
    ["B", "Y", "W", "W", "B"],
    ["B", "W", "B", "W", "B"],
    ["B", "W", "W", "W", "B"],
    ["B"] * 5,
]

def update_maze():
    cx, cy = 1, 1
    while True:
        for x, y in ( (1,1), (1,2), (1,3), (2,3), (3,3), (3,2), (3,1), (2,1)):
            maze[cx][cy] = "W"
            maze[x][y] = "Y"
            cx, cy = x,y
            time.sleep(1)

class MyServer(BaseHTTPRequestHandler):
    def do_GET(self):
        self.send_response(200)
        self.send_header("Access-Control-Allow-Origin", "*")
        self.send_header("Content-type", "text/html")
        self.end_headers()
        # tmp = "\n".join("".join(row) for row in maze)
        self.wfile.write(bytes(json.dumps(maze).encode("utf-8")))


if __name__ == "__main__":
    webServer = HTTPServer((hostName, serverPort), MyServer)
    _thread.start_new_thread(update_maze, ())
    try:
        print("Server started http://%s:%s" % (hostName, serverPort))
        webServer.serve_forever()
    except KeyboardInterrupt:
        pass
    webServer.server_close()
    print("Server stopped")