diff options
| author | jullinator <justus.karlsson@hotmail.se> | 2018-08-25 02:03:46 +0200 |
|---|---|---|
| committer | jullinator <justus.karlsson@hotmail.se> | 2018-08-25 02:03:46 +0200 |
| commit | e3b6cb5c1cf67286409ae5f9789f203e6beddff1 (patch) | |
| tree | 40df5783f85193b885cffc3c1cc8efc4bfdf61b0 /server.py | |
| parent | b72e4ee4ae112beb4894c4cdb4cf69c6c8dcc061 (diff) | |
| download | tdde25-e3b6cb5c1cf67286409ae5f9789f203e6beddff1.tar.gz | |
new structure
Diffstat (limited to 'server.py')
| -rw-r--r-- | server.py | 58 |
1 files changed, 1 insertions, 57 deletions
@@ -1,57 +1 @@ -from http.server import HTTPServer, BaseHTTPRequestHandler -import lib - - - -class Request(): - def __init__(self, body): - self.body = body - - -# Explanation: https://blog.anvileight.com/posts/simple-python-http-server/ - -class Handler(BaseHTTPRequestHandler): - """ The class responsible for handling all the - different requests made to our server - """ - - def _set_headers(self): - """ Standard header stuff that has to be done """ - self.send_response(200) - self.send_header("Content-type", "text/html") - self.end_headers() - - def do_GET(self): - """ Just serve up our one and only html page """ - self._set_headers() - res = lib.find_get(self.path) - if hasattr(res, 'encode'): - self.wfile.write(res.encode()) - else: - self.wfile.write(res) - - - def do_POST(self): - """ An async ajax request. Find the function from 'posts.py' to handle this - particular request. - """ - self.send_response(200) - self.end_headers() - content_length = int(self.headers['Content-Length']) - body = self.rfile.read(content_length) - request = Request(body) - res = lib.find_post(self.path, request) - self.wfile.write(res.encode()) - - -def run (port = 8314): - server_adress = ('', port) # Should make the port a command line argument - server = HTTPServer(server_adress, Handler) - print(f'Starting server on port: {port}.') - server.serve_forever() - - - -if __name__ == '__main__': - run() - +## Work yo magic xDDDD
\ No newline at end of file |
