Update myServer.py

This commit is contained in:
siwinter 2019-10-31 01:25:38 +01:00 committed by GitHub
parent 5748f1b182
commit f1cd3d7198
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,27 +1,65 @@
#!/usr/bin/python
from BaseHTTPServer import BaseHTTPRequestHandler,HTTPServer
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# myServer.py
#
# Copyright 2019 swinter <swinter@T420>
#
# 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("<html><head><title>Title goes here.</title></head>".encode("utf-8"))
self.wfile.write("<body><p>This is a test.</p>".encode("utf-8"))
# If someone went to "http://something.somewhere.net/foo/bar/",
# then s.path equals "/foo/bar/".
self.wfile.write(("<p>You accessed path: %s</p>" % self.path).encode("utf-8"))
self.wfile.write("</body></html>".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))