From f1cd3d7198a456b6001ce03bdea0215aa2e4f9bf Mon Sep 17 00:00:00 2001 From: siwinter <45730097+siwinter@users.noreply.github.com> Date: Thu, 31 Oct 2019 01:25:38 +0100 Subject: [PATCH] Update myServer.py --- myServer.py | 84 ++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 61 insertions(+), 23 deletions(-) diff --git a/myServer.py b/myServer.py index 2620112..396f8d0 100644 --- a/myServer.py +++ b/myServer.py @@ -1,27 +1,65 @@ -#!/usr/bin/python -from BaseHTTPServer import BaseHTTPRequestHandler,HTTPServer +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +# +# myServer.py +# +# Copyright 2019 swinter +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, +# MA 02110-1301, USA. +# +# +import logging +from http.server import BaseHTTPRequestHandler +import socketserver -PORT_NUMBER = 8080 +PORT = 7080 -#This class will handles any incoming request from -#the browser -class myHandler(BaseHTTPRequestHandler): +class MyHandler(BaseHTTPRequestHandler): + def do_HEAD(self): + self.send_response(200) + self.send_header("Content-type", "text/html") + self.end_headers() + def do_GET(self): + """Respond to a GET request.""" + self.send_response(200) + self.send_header("Content-type", "text/html") + self.end_headers() + self.wfile.write("Title goes here.".encode("utf-8")) + self.wfile.write("

This is a test.

".encode("utf-8")) + # If someone went to "http://something.somewhere.net/foo/bar/", + # then s.path equals "/foo/bar/". + self.wfile.write(("

You accessed path: %s

" % self.path).encode("utf-8")) + self.wfile.write("".encode("utf-8")) + return - #Handler for the GET requests - def do_GET(self): - self.send_response(200) - self.send_header('Content-type','text/html') - self.end_headers() - # Send the html message - self.wfile.write("Hello World !") - print self.path - return -try: - #Create a web server and define the handler to manage the - #incoming request - server = HTTPServer(('', PORT_NUMBER), myHandler) - print 'Started httpserver on port ' , PORT_NUMBER - - #Wait forever for incoming htto requests - server.serve_forever() +def main(args): + + logging.basicConfig(filename='myServer.log',level=logging.DEBUG) + logging.basicConfig(format='%(asctime)s %(message)s') + logging.info('myServer start') + httpd = socketserver.TCPServer(("", PORT), MyHandler) + logging.info("myServer serving at port " + str(PORT)) + try: + httpd.serve_forever() + except KeyboardInterrupt: + pass + httpd.server_close() + logging.info('myServer closed') + +if __name__ == '__main__': + import sys + sys.exit(main(sys.argv))