Start writing the server component

This commit is contained in:
Juhani Krekelä 2018-03-18 21:33:16 +02:00
parent 7a56c20e80
commit 8e363f80de
1 changed files with 31 additions and 0 deletions

31
server.py Normal file
View File

@ -0,0 +1,31 @@
import http.server
import urllib.parse
class HTTPRequestHandler(http.server.BaseHTTPRequestHandler):
server_version = 'Buranun/0.0'
protocol_version = 'HTTP/1.1'
def __send_plaintext(self, string):
if string[-1:] != '\n':
string += '\n'
encoded = string.encode('utf-8')
length = len(encoded)
self.send_response(200)
self.send_header('Content-Type', 'text/plain; charset=utf-8')
self.send_header('Content-Length', length)
self.end_headers()
self.wfile.write(encoded)
def do_GET(self):
path = urllib.parse.unquote(self.path)
self.__send_plaintext(path)
def main():
httpd = http.server.HTTPServer(('', 8000), HTTPRequestHandler)
httpd.serve_forever()
if __name__ == '__main__':
main()